Question

In: Computer Science

The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...

The purpose here is to implement the QuickSort sorting algorithm to sort integers.

Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3.

Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less than 10 elements. Your C program should also output the sorted list of integers, one per line. The last two lines of output should be the Number of Comparisons and the Number of Swaps (with appropriate labels)

Solutions

Expert Solution

//project3.c

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

#define MAX_ELEMENTS 1024 //change it if required

int Partition(int *array, int l, int r);
int QuickSort(int *array, int l, int r);
int comparisons = 1;
int no_of_swap = 0;

void swap(int *array, int index1, int index2)
{
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
    no_of_swap++;
}

int Partition(int *array, int l, int r)
{
    int n = r - l;
    int middle = ((n - (n % 2)) / 2) + l;

    // order left and middle value
    if(array[l] > array[middle]) {
        swap(array, l, middle);
    }
    // order left and right values
    if(array[l] > array[r]) {
        swap(array, l, r);
    }
    // order middle and right values
    if(array[middle] > array[r]) {
        swap(array, middle, r);
    }
    swap(array, l, middle);
    int p = array[l];
    int i = l + 1;
    int end = 0;
    int j;
    for (j = l + 1; j <= r; j++)
    {
        if (array[j] < p){
            int k = array[j];
            array[j] = array[i];
            array[i] = k;
            i++;
        }
        end = j;
    }
    //swap A[l] and A[i-1]
    int a = array[l];
    array[l] = array[i-1];
    array[i-1] = a;
    QuickSort(array, l, i - 2);
    QuickSort(array, i, end);
}

int QuickSort(int *array, int l, int r)
{
    if (l < r)
    {
        Partition(array, l, r);
        comparisons = comparisons + (r - l);
    }
    else
    {
        return 0;
    }
}
void main(int argc, char *argv[] )
{

   char *file_name;
   int array[MAX_ELEMENTS];
   FILE *fp;
   int i=0;
   int j = 0;

   if(argc < 2){
      printf("invalid argument \n");
       return;
   }
       else{
       file_name = malloc(strlen(argv[1]));
       strcpy(file_name,argv[1]);
   }
  
   fp = fopen(file_name,"r");
   if(fp == NULL) {
            printf("Unable to open file!");
            return;
   }


   char chunk[128];  
   while(fgets(chunk, sizeof(chunk), fp) != NULL) {
       array[i] = atoi(chunk);
       i++;
      
   }
  
   QuickSort(array, 0, i - 1);

   for(j=0;j<i;j++){
       printf("%d\n",array[j]);
   }

   printf("comparisons = %d\nSwap = %d\n",comparisons,no_of_swap);
}


Related Solutions

Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
1a)Use the Lomuto's quicksort algorithm to sort the following list of integers in nondecreasing order for...
1a)Use the Lomuto's quicksort algorithm to sort the following list of integers in nondecreasing order for the first pivot item. Use pointers i and j for swapping items (if necessary) for each step leading to your answer. Note do the problem just for the first pivot item. 123,34,189,56,150,12,9,24 1b)Use the same list of numbers and use the Hoare's quicksort algorithm using pointers l and r and updating the pointers i and j and swapping items. Do the problem just for...
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Implement Quicksort in Java by sorting in parallel after partitioning; for this, the parent thread can...
Implement Quicksort in Java by sorting in parallel after partitioning; for this, the parent thread can continue sorting one segment and a child thread is created for sorting the other segment. However, create a new thread only if both segments contain more than S elements, otherwise sort sequentially both segments. %%writefile Quicksort.java import java.util.Random; YOUR CODE HERE public class Quicksort { static int N; // number of elements to be sorted static int S; // threshold for creating a sub-thread...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT