Question

In: Computer Science

Write a program that uses two identical arrays of at least 25 integers. It should call...

Write a program that uses two identical arrays of at least 25 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in descending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on the screen.

Solutions

Expert Solution


/*
 *  C Language Program
 *  Count number of swaps in bubble sort and selectoin sort
 */

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define MAX 25

int bubbleSort(int array[], int count);
int selectionSort(int arr[], int N);

int main()
{
  int arrayFirst[MAX];
  int arraySecond[MAX];
  int bubbleSwap, selectionSwap;
  int i, temp;

  srand(time(NULL));

  for (i = 0; i < MAX; i++)
  {
    temp = rand() % 100;
    arrayFirst[i] = temp;
    arraySecond[i] = temp;
  }
  
  bubbleSwap = bubbleSort(arrayFirst, MAX);
  selectionSwap = selectionSort(arraySecond, MAX);

  printf("Total number of swaps in bubble sort: %d\n", bubbleSwap);
  printf("Total number of swaps in selection sort: %d\n", selectionSwap);

  return 0;
}

int bubbleSort(int arr[], int N)
{
  int i, j, temp;
  int swaps = 0;
  
  for (i = 0; i < N - 1; ++i)
  {
    for (j = 0; j < N - 1 - i; ++j)
    {
      if (arr[j] > arr[j+1])
      {
        temp = arr[j + 1];
        arr[j + 1] = arr[j];
        arr[j] = temp;
        swaps++;
      }
    }
  }

  return swaps;
}

int selectionSort(int arr[], int N)
{
   int i, j;
  int temp, min_indx;
  int swaps = 0;
   
   for(i = 0; i < N; i++)
   {
      min_indx = i;
      
      for(j = i+1; j < N; j++)
      {
         if(arr[min_indx] > arr[j])
            min_indx = j;
      }
      
    temp = arr[min_indx];
    arr[min_indx] = arr[i];
    arr[i] = temp;
      swaps++;
   }

  return swaps;
}

/*  Program ends here */

Note: No language was mentioned, hence the program is written in C, for change of language drop a comment.


Related Solutions

In C++, write a program that uses two identical arrays of ten randomly ordered integers. It...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It should display the contents of the first array, then call a function to sort it using the most efficient descending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using descending order selection sort, modified to print out the...
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Q-1) Write a program that mimics a calculator. The program should take as input two integers...
Q-1) Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. For division, if the denominator is zero, output an appropriate message. Some sample outputs follow: 3 + 4 = 7 13 * 5 = 65
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays....
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays. splice() must create the third array and return a pointer to main(). Main() must capture the pointer returned from the splice() function. Sol'n so far, I get the results of the first and second array, but it isn't showing the results of the spliced function: #include <iostream> using namespace std; // Function int* splice() inserts the 2nd array into 1st array starting at int...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT