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.
Compute the first 4 pivots using quick sort algorithm on an array A=<10,15,17,18,6,120,30,250,95>. Write the content...
Compute the first 4 pivots using quick sort algorithm on an array A=<10,15,17,18,6,120,30,250,95>. Write the content of the array A after each pivot is chosen.
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.
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e.,...
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e., from pass 1 to pass 7). Each pass is defined as the completion of one partition. We always pick the first array element as the pivot for each partition.
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
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
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1,...
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1, 0, 2, 1, 5, 0  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT