Question

In: Computer Science

Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm....

Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm. show work

Solutions

Expert Solution

Program in c++:

#include<iostream> 
using namespace std;  
void swap(int* a, int* b)  //function to swap two element
{  
    int temp = *a;  
    *a = *b;  
    *b = temp;  
}  
  
int partition (int arr[], int low, int high)  
{  
    int pivot = arr[high]; //take last element as pivot  
    int i = (low-1); // Index of smaller element  
  
    for (int j = low; j <= high - 1; j++)  
    {  
         
        if (arr[j] < pivot)// If arr[j] is smaller than the pivot 
        {  
            i++; // increment index of smaller element  
            swap(&arr[i], &arr[j]);  
        }  
    }  
    swap(&arr[i + 1], &arr[high]);  // places  the pivot element at its correct position in sorted array
    return (i + 1);  // return partition index
}  
  
void quickSort(int arr[], int low, int high)  //low = Starting index,high = Ending index 
{  
    if (low < high)  
    {  
        int q = partition(arr, low, high);  
        quickSort(arr, low, q - 1);  // Separately sort elements before  partition 
        quickSort(arr, q + 1, high); // Separately sort elements and after partition
    }  
}  
  
int main()  
{  
    int arr[] = {33,77,22,11,34,21,88,90,42};  
    int n = sizeof(arr) / sizeof(arr[0]);  
    quickSort(arr, 0, n - 1);  
    cout << "Sorted array is :";  
    int i;  
    for (i = 0; i < n; i++){ 
        cout<<arr[i]<<" ";  
    }
    
    return 0;  
}  

Output:

Algorithm for sort function

here,

  1. low is a starting index and high is ending index
  2. arr is a array.
  3. q is a index partition index

QuickSort(arr[],low,high)

{

if(low<high){  

q=Partition(arr,low,high);

QuickSort(arr,low,q-1); // sort before partition index (q)

QuickSort(arr,q+1,high);  // sort after partition index (q)

}

}

Algorithm for Partition function:

Partition(arr[],low,high)

{

pivot=arr[high];

i=low-1;

for(j=low;j<=high-1;j++){

if(arr[j]<pivot){

i=i+1;

swap arr[i] and arr[j]

}

}

swap arr[i+1] and arr[high]

return i+1

}

demonstration For partition function:

int arr[] = {33,77,22,11,34,21,88,90,42}

low=0,high=8,pivot=arr[high]=42

i=-1

j loop from low to high-1

j=0, check arr[j]<=pivot which istrue, increase i by 1(i=0) swap (arr[i],arr[j])

arr[]={33,77,22,11,34,21,88,90,42}

j=1 check arr[j]<=pivot which is false . no change

j=2 check arr[j]<=pivot which is true increase i by 1(i=1) swap (arr[i] ,arr[j])

arr[]={33,22,77,11,34,21,88,90,42}

j=3 check arr[j]<=pivot which is true increase i by 1(i=2) swap (arr[i] ,arr[j])

arr[]={33,22,11,77,34,21,88,90,42}

j=4 check arr[j]<=pivot which is true increase i by 1(i=3) swap (arr[i] ,arr[j])

arr[]={33,22,11,34,77,21,88,90,42}

j=5 check arr[j]<=pivot which is true increase i by 1(i=4) swap (arr[i] ,arr[j])

arr[]={33,22,11,34,21,77,88,90,42}

j=6 check arr[j]<=pivot which is false . no change

j=7  check arr[j]<=pivot which is false . no change

j loop is end and value of j=8 and i=4

swap arr[i+1] and arr[high]

arr[]={33,22,11,34,21,42,88,90,77}

return index of 42 which is 5 to QuickSort function (42 at its correct position in sorted array)

Now In QuickSort function again call for before and after partition index. This process is done recursivly until low<high

At last we get sorted array


Related Solutions

Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Selection sort and shell sort....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Selection sort and shell sort. Write the algorithms. show work. please
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write...
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write the algorithm.
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement...
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement in the algorithm. (You must explain your reason for each statement about how you get the answer of each computation time in at one or two sentences each.)
Given the following data set 11 20 33 45 52 30 27 21 38 42 28...
Given the following data set 11 20 33 45 52 30 27 21 38 42 28 25 79 60 14 35 100 23 88 58 A. Find the quartiles B. Determine if there are any outliers C. Draw a box plot (exclude outliers but plot them as well).
Sort 101, 55, 64, 23, 12, 09, 45, 32, 19, 65, 89 using Quick sort. Write...
Sort 101, 55, 64, 23, 12, 09, 45, 32, 19, 65, 89 using Quick sort. Write the algorithm. Please show steps.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT