forked from usamajay/hacktoberfest2021-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhybridisation_quicksort_insertionsort.cpp
More file actions
63 lines (63 loc) · 1.65 KB
/
hybridisation_quicksort_insertionsort.cpp
File metadata and controls
63 lines (63 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
using namespace std;
void insertion_sort(int a[],int n)
{
for(int i=1;i<n;i++)
{
int temp=a[i];
int j=i-1;
while(j>=0&&a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}
int partition_scheme(int a[],int p,int r)
{
int pivot=a[0];
while(p<r)
{
while(a[p]<=pivot)
p++;
while(a[r]>pivot)
r--;
if(p<r)
{
int temp=a[p];
a[p]=a[r];
a[r]=temp;
}
}
int temp=a[0];
a[0]=a[r];
a[r]=temp;
return r;
}
void quick_sort(int a[],int p,int r)
{
if(p<r)
{
int q=partition_scheme(a,p,r);
quick_sort(a,p,q-1);
quick_sort(a,q+1,r);
}
}
int main()
{
int a[200],n;
cout<<"Enter the number of elements:";
cin>>n;
cout<<"Enter the array:\n";
for(int i=0;i<n;i++)
cin>>a[i];
if(n<=10)
insertion_sort(a,n);
else
quick_sort(a,0,n);
cout<<"Sorted Array:";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}