-
Notifications
You must be signed in to change notification settings - Fork 0
/
ollie124.cpp
106 lines (94 loc) · 2.01 KB
/
ollie124.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<bits/stdc++.h>
using namespace std;
void Insertion_Sort(int arr[] , int first , int last)
{
for(int i = first + 1 ; i < last + 1 ; i++)
{
int val = arr[i];
int j = i;
while (j > first && arr[j-1] > val)
{
arr[j] = arr[j-1];
j -= 1;
}
arr[j] = val;
}
}
int Partition(int arr[] , int first , int last)
{
int pivot = arr[last];
int i , j;
i = first;
j = first;
for(int i = first ; i < last ; i++)
{
if(arr[i] < pivot)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j += 1;
}
}
int temp = arr[j];
arr[j] = arr[last];
arr[last] = temp;
return j;
}
void Quick_Sort(int arr[] , int first , int last)
{
if(first < last)
{
int pivot = Partition(arr , first , last);
Quick_Sort(arr , first , pivot - 1) ;
Quick_Sort(arr , pivot + 1 , last) ;
}
}
// Hybrid function is a combination of Quick and Insertion sort
void Hybrid_QuickSort(int arr[] , int first , int last)
{
while(first < last)
{
// If the size of the array is less
// than 10 apply insertion sort
// and stop recursion
if(last - first + 1 < 10)
{
Insertion_Sort(arr , first , last);
break;
}
else
{
int pivot = Partition(arr , first , last) ;
if(pivot - first < last - pivot)
{
Hybrid_QuickSort(arr , first , pivot - 1);
first = pivot + 1;
}
else
{
Hybrid_QuickSort(arr , pivot + 1 , last);
last = pivot - 1;
}
}
}
}
int main()
{
int size;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
cout << "Enter the elements of the array: " << endl;
for(int i = 0 ; i < size ; i++)
{
cin >> arr[i];
}
Hybrid_QuickSort(arr, 0, size - 1);
for(int i = 0 ; i < size ; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}