0% found this document useful (0 votes)
36 views13 pages

Ds 9queues and RecursionQuickSort

The document describes the quicksort algorithm for sorting an array. It explains that quicksort is a recursive algorithm that works by partitioning the array around a pivot element and recursively sorting the subarrays. The key steps are: 1) Choose a pivot element, 2) Partition the array by putting elements less than the pivot before it and greater elements after it, and 3) Recursively apply the same process to the subarrays on each side of the pivot until the entire array is sorted. Examples are provided to illustrate how the algorithm works on sample arrays. The time complexity of quicksort is also discussed as being O(n log n) on average and in the best case, but can be O(n^2) in the worst

Uploaded by

nulla
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
36 views13 pages

Ds 9queues and RecursionQuickSort

The document describes the quicksort algorithm for sorting an array. It explains that quicksort is a recursive algorithm that works by partitioning the array around a pivot element and recursively sorting the subarrays. The key steps are: 1) Choose a pivot element, 2) Partition the array by putting elements less than the pivot before it and greater elements after it, and 3) Recursively apply the same process to the subarrays on each side of the pivot until the entire array is sorted. Examples are provided to illustrate how the algorithm works on sample arrays. The time complexity of quicksort is also discussed as being O(n log n) on average and in the best case, but can be O(n^2) in the worst

Uploaded by

nulla
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 13

Quick sort using recursive

method
Recursive Algorithm
Example
Quick sort complexity
Quick Sort: Example

2
Example of Quick Sort...

3
Quicksort Algorithm
• Input: A[1, …, n]
• Output: A[1, .., n], where A[1]<=A[2]…<=A[n]
• Quicksort:
1. if(n<=1) return;
2. Choose the pivot p = A[n]
3. Put all elements less than p on the left; put all elements lager
than p on the right; put p at the middle. (Partition)
4. Quicksort(the array on the left of p)
5. Quicksort(the array on the right of p)
Quicksort Algorithm
• Quicksort example Current pivots

Previous pivots
2 8 7 1 3 5 6 4
Quicksort
2 1 3 4 7 5 6 8

2 1 3 4 7 5 6 8

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8
Recursive Algorithm cont…
QUICKSORT(A, p, r)
1. if p < r
2. q =PARTITION(A, p, r)
3. QUICKSORT(A, p, q-1)
4. QUICKSORT(A, q +1, r)
5. Exit
Quicksort Algorithm
• Partition example
tail

2 8 7 1 3 5 6 4
tail

2 8 7 1 3 5 6 4 Exchange 2 with A[tail+1]


tail

2 8 7 1 3 5 6 4 Do nothing

tail

2 8 7 1 3 5 6 4 Do nothing
Quicksort Algorithm
• Partition example
tail

2 8 7 1 3 5 6 4 Exchange 1 with A[tail+1]

tail

2 1 7 8 3 5 6 4 Exchange 3 with A[tail+1]


tail

2 1 3 8 7 5 6 4 Do nothing
tail

2 1 3 8 7 5 6 4 Do nothing

2 1 3 4 7 5 6 8 Final step: exchange A[n]


with A[tail+1]
Recursive Algorithm(partition)
PARTITION(A, p, r)
1. pivot = A[r]
2. tail = p-1
3. for j = p to r – 1
4. if A[j]<=pivot
5. tail=tail+1
6. exchange A[tail]with A[j] [End of If block]
[End of For Loop]
7. exchange A[tail+1] with A[r]
8. return tail + 1
Example:

i=tail
Complexity of Quick sort
Worst case
O(n2)

Average case
O(n log n)

Best case
O(n log n)
#include<iostream> void QuickSort(int arr[], int p, int r)
{
using namespace std; if (p < r) {
int partition(int arr[], int p, int r)
{ int q = partition(arr,p,r);
int pivot=arr[r];
QuickSort(arr, p, q-1);
int tail=p-1;
for(int j=p; j<r;j++) QuickSort(arr, q + 1, r);
{
}
if(arr[j]<=pivot) }
{ int main()
tail=tail+1; {
int temp= arr[j]; int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
arr[j]=arr[tail];
arr[tail]=temp; cout<<"Given array is \n";
} for(int i=0;i<arr_size;i++)
cout<<arr[i]<<"\t";
}
int temp=arr[r]; QuickSort(arr, 0, arr_size - 1);
arr[r]=arr[tail+1];
arr[tail+1]=temp; cout<<"\nSorted array is \n";
for(int i=0;i<arr_size;i++)
return tail+1; cout<<arr[i]<<"\t";
} return 0;
}
Thank You

You might also like