Question

In: Computer Science

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.

Solutions

Expert Solution

Solutions:-

code:-

#include <stdio.h>
#include<conio.h>
void swap(int *a, int *b)
{
int t=*a;
*a=*b;
*b=t;
}

int partition(int arr[],int l,int h)
{
int p=arr[h];
int i=(l-1);
for(int j=l;j<h;j++)
{
if(arr[j]<=p)
   {
i++;
swap(&arr[i],&arr[j]);
}
}
   swap(&arr[i+1],&arr[h]);
   return (i+1);
}

void Quick_Sort(int arr[],int l,int h)
{
if(l<h)
{
   int pi=partition(arr,l,h);
   Quick_Sort(arr,l,pi-1);
   Quick_Sort(arr,pi+1,h);
}
}

void printing(int arr[],int s)
{
for(int i=0;i<s;i++)
{
printf("%d\n",arr[i]);
}
}

int main()
{
int data[] = {101, 55, 64, 23, 12, 9, 45, 32, 19, 65, 89};
int n=(sizeof(data)/sizeof(data[0]));
Quick_Sort(data,0,n-1);
printf("Sorted array:\n");
printing(data,n);
}

output:

Algorithm:-

Quick_Sort(arr, LMI, RMI)

            if (LMI <RMI)

                        PI <- partition(arr, LMI, RMI)

                        Quick_Sort(arr, LMI, PI)

                        Quick_Sort(arr, PI + 1, RMI)

partition(arr,LMI, RMI)

set RMI as PI

            store_Index<-LMI - 1

            for (i <- LMI + 1 to RMI)

                        if (element[i] < P_Element)

                        swap element[i] and element[store_Index]

store_Index++

swap P_Element and element[store_Index+1]

return store_Index + 1

NOte:-

LMI=Left Most Index

RMI=Right Most Index

P_Element=Pivot element

PI=pivot index

arr=Array


Related Solutions

Provide all steps b) Sort the keys 20, 31, 29, 45, 09, 98, 12, 23, 99,...
Provide all steps b) Sort the keys 20, 31, 29, 45, 09, 98, 12, 23, 99, 66 in descending order using the Heap sort. Also write the algorithm. a) Sort 99, 54, 64, 23, 12, 09, 45, 32, 19, 65, 89 using Quick sort. Write the algorithm.                                                                    b) Sort 69, 74, 64, 23, 12, 09, 45, 32, 19, 65, 88, 33, 60, 38 using Shell sort. Write the algorithm.
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.
Given the data set below 25 52 67 23 78 89 57 90 32 77 45...
Given the data set below 25 52 67 23 78 89 57 90 32 77 45 48 62 54 94 69 46 79 40 33 21 57 84 54 23 34 68 63 61 76 87 78 39 50 70 60 32 65 73 45 28 82 66 79 71 80 46 66 24 90 A)What is the probability of an impossible even? B) What is the probability of a certain event? C) find the approximate mean using the Frequency...
How to write a quick sort using just one recursive call in Java?
How to write a quick sort using just one recursive call in Java?
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
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.
The actual values for 12 periods (shown in order) are: (1) 45 (2) 52 (3) 48 (4) 59 (5) 55 (6) 55 (7) 64 (8) 58 (9) 73 (10) 66 (11) 69 (12) 74
Question 1 contains the actual values for 12 periods (listed in order, 1-12). In Excel, create forecasts for periods 6-13 using each of the following methods: 5 period simple moving average; 4 period weighted moving average (0.63, 0.26, 0.08, 0.03); exponential smoothing (alpha = 0.23 and the forecast for period 5 = 53); linear regression with the equation based on all 12 periods; and quadratic regression with the equation based on all 12 periods.  Round all numerical answers to two...
1.The actual values for 12 periods (shown in order) are:(1) 45 (2) 52 (3) 48 (4) 59 (5) 55 (6) 55 (7) 64 (8) 58 (9) 73 (10) 66 (11) 69 (12) 74
  Question 1 contains the actual values for 12 periods (listed in order, 1-12). In Excel, create forecasts for periods 6-13 using each of the following methods: 5 period simple moving average; 4 period weighted moving average (0.63, 0.26, 0.08, 0.03); exponential smoothing (alpha = 0.23 and the forecast for period 5 = 53); linear regression with the equation based on all 12 periods; and quadratic regression with the equation based on all 12 periods.  Round all numerical answers to two...
{4, 6, 12, 15, 19, 23, 24}   Write one statement to print out (cout) the first...
{4, 6, 12, 15, 19, 23, 24}   Write one statement to print out (cout) the first character and the last character of the vector you initialized Write one statement to insert an integer 9 to myvec between numbers 6 and 12. in c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT