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

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...
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!
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation...
Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation , all numbers must be between 0 and 100) 2) find the largest number 3) Find the Smallest Number 4) Find the Sum of all numbers in the Array Display: as per the user's section (menu using switch or if else ) Largest Number Smallest Number Sum of all numbers the original Array DO NOT USE METHODS OR FUNCTIONS Submit: Source code (C++) output...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
Write a program that uses a custom function to generate a specified number of random integers...
Write a program that uses a custom function to generate a specified number of random integers in a specified range. This custom function should take three arguments; the number of integers to generate, the lower limit for the range, and the upper limit for the range. Values for these arguments should be entered by the user in main. The custom function should display the random integers on one line separated by single spaces. The function should also report how many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT