Professional Documents
Culture Documents
Recursive Algorithm Quick Sort Complexity: Example
Recursive Algorithm Quick Sort Complexity: Example
Example
Quick sort complexity
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm cont…
QUICKSORT(A, first, last)
1. if first< last
2. q =PARTITION(A, first, last)
3. QUICKSORT(A, first, q-1)
4. QUICKSORT(A, q +1, last)
5. Exit
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Recursive Algorithm(partition)
PARTITION(A, first, last)
1. x = A[last]
2. i = first-1
3. for j = first to last – 1
4. if A[j]<=x
5. i=i+1
6. exchange A[i]with A[j]
7. exchange A[i+1] with A[last]
8. return i + 1
Example:
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
void swap(int arr[],int i, int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
int partition(int arr[],int first,int last)
{
int pivot=arr[last];
int i=first-1;
for(int j=first;j<last;j++)
{
if(arr[j]<pivot)
{
i++;
swap(arr,i,j);
}
}
swap(arr,i+1,last);
return i+1;
}
void quicksort(int arr[],int first,int last)
{
if(first<last)
{
int pi=partition(arr,first,last);
quicksort(arr,first,pi-1);
quicksort(arr,pi+1,last);
}
}
int main()
{
int arr[5];
for(int i=0;i<5;i++)
{
for(int i=0;i<5;i++)
{
cout<<arr[i]<<"\n";
}
Thank You