Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //quicksort
- #include<bits/stdc++.h>
- #include<stdlib.h>
- using namespace std;
- int num[100];
- void Quicksort(int f,int l)
- {
- int i,j;
- if(f<l)
- {
- i=f+1;
- j=l;
- while(num[i]<num[f])
- {
- i++;
- }
- while(num[j]>num[f])
- {
- j--;
- }
- while(i<j)
- {
- swap(num[i],num[j]);
- while(num[i]<num[f])
- {
- i++;
- }
- while(num[j]>num[f])
- {
- j--;
- }
- }
- swap(num[j],num[f]);
- Quicksort(f,j-1);
- Quicksort(j+1,l);
- }
- }
- int main()
- {
- int n;
- cout<<"Enter the numbers:";
- cin>>n;
- for(int i=0; i<n; i++)
- num[i]=rand()%1000;
- cout<<"the unsorted list:";
- for(int i=0; i<n; i++)
- cout<<num[i]<<" ";
- cout<<endl;
- Quicksort(0,n-1);
- cout<<"The sorted list is:";
- for(int i=0; i<n; i++)
- cout<<num[i]<<" ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement