Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
436 views

Sorting Using Quick Sort Algorithm

The document describes the quicksort algorithm for sorting an array. It explains that quicksort works by picking a pivot element and partitioning the array around that element such that all elements less than the pivot come before it and all elements greater than the pivot come after it. It then recursively applies this process to the subarrays until the entire array is sorted. The document provides pseudocode for quicksort and walks through an example of sorting an array using quicksort.

Uploaded by

Lakshay Garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
436 views

Sorting Using Quick Sort Algorithm

The document describes the quicksort algorithm for sorting an array. It explains that quicksort works by picking a pivot element and partitioning the array around that element such that all elements less than the pivot come before it and all elements greater than the pivot come after it. It then recursively applies this process to the subarrays until the entire array is sorted. The document provides pseudocode for quicksort and walks through an example of sorting an array using quicksort.

Uploaded by

Lakshay Garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sorting using Quick Sort Algorithm:

/* a[] is the array, p is starting index, that is 0, and r is the last index of
array. */ "
void quicksort(int a[], int p, int r)
{
if(p < r)
{
int q;
q = partition(a, p, r);
quicksort(a, p, q);
quicksort(a, q+1, r);
}
}
int partition(int a[], int p, int r)
{
int i, j, pivot, temp;
pivot = a[p];
i = p;
j = r;
while(1)
{
while(a[i] < pivot && a[i] != pivot)
i++;
while(a[j] > pivot && a[j] != pivot)
j--;
if(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
return j;
}
}

Example of Quick Sort:


44  33  11  55  77  90  40  60  99  22  88   66

1.) Let 44 be the Pivot element and perform searching right to left

2.) Comparing 44 to the right hand -side elements, and if the element
present in right-side elements is  smaller than 44 , then swap it. As 22 is
smaller than 44 so swap them.
22 33 11 55 77 90 40 60 99 44 88 66

3.) Now comparing 44 to the left side element and the element present on
the left hand side must be greater than 44 then swap them. As 55 is
greater than 44 so swap them.
22 33 11 44 77 90 40 60 99 55 88 66

4.) Repeating steps 1 & steps 2 until we get two lists one from the left of
pivot element 44 & one to the right hand side of pivot element.
22 33 11 40 77 90 44 60 99 55 88 66

5.) Swap with 77:


22 33 11 40 44 90 77 60 99 55 88 66
Now, the element on the right side and left side are greater than and
smaller than 44 respectively.

6.)Now we get two sorted lists:

22 33 11 40 44 90 77 60 99 55 88 66
(sublist 1) (Sublist 2)

And these sublists are sorted under the same steps as above done.
These two sorted sublists side by side.
First sublist:
22,33,11,40 in which i=22,j=40 and pivot=22 so scan right to left simply 11
is less than “<” pivot so interchange list :

11,22,33,40 so this sublist is sorted .

now take the second sublist:

90,77,60,99,55,88,66
so here i=90(pivot) and j=66 so here j= 66 is less than 90 so interchange in
right to left scan

see list:

66,77,60,99,55,88,90

now left to right scan 99>90 so interchange so now list :

66,77,60,90,55,88,99

now right to left scan 88<90 so list:

66,77,60,88,55,90,99 so here again we found two sublist .

66,77,60,88,55 and 90,99 because all elements left side of pivot less than
pivot and right side greater than pivot so again take first sublist

66,77,60,88,55 here i=66(pivot) and j=55 so scan right to left 55<66 so


interchange then list:

55,77,60,88,66 

so now scan the left to right 77>66 so list:

55,66,60,88,77

so now scan the right to left 60<66 so list:

55,60,66,88,77

so again two sublist 55,60 and 88,77

so take second sublist 88,77 so scan the right to left so no element<66 so


list: 88,77

so now scan left to right 77<88 so list: 77,88 so we have


55,60,66,77,88,90,99
so finally sorted list :

11,22,33,40,44,55,60,66,77,88,90,99

You might also like