Skip to content

Commit f075906

Browse files
committed
Added Introsort
1 parent e00a889 commit f075906

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

cpp/sorting/Introsort.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// A utility function to swap the values pointed by
5+
// the two pointers
6+
void swapValue(int* a, int* b)
7+
{
8+
int* temp = a;
9+
a = b;
10+
b = temp;
11+
return;
12+
}
13+
14+
/* Function to sort an array using insertion sort*/
15+
void InsertionSort(int arr[], int* begin, int* end)
16+
{
17+
// Get the left and the right index of the subarray
18+
// to be sorted
19+
int left = begin - arr;
20+
int right = end - arr;
21+
22+
for (int i = left + 1; i <= right; i++) {
23+
int key = arr[i];
24+
int j = i - 1;
25+
26+
/* Move elements of arr[0..i-1], that are
27+
greater than key, to one position ahead
28+
of their current position */
29+
while (j >= left && arr[j] > key) {
30+
arr[j + 1] = arr[j];
31+
j = j - 1;
32+
}
33+
arr[j + 1] = key;
34+
}
35+
36+
return;
37+
}
38+
39+
// A function to partition the array and return
40+
// the partition point
41+
int* Partition(int arr[], int low, int high)
42+
{
43+
int pivot = arr[high]; // pivot
44+
int i = (low - 1); // Index of smaller element
45+
46+
for (int j = low; j <= high - 1; j++) {
47+
// If current element is smaller than or
48+
// equal to pivot
49+
if (arr[j] <= pivot) {
50+
// increment index of smaller element
51+
i++;
52+
53+
swap(arr[i], arr[j]);
54+
}
55+
}
56+
swap(arr[i + 1], arr[high]);
57+
return (arr + i + 1);
58+
}
59+
60+
// A function that find the middle of the
61+
// values pointed by the pointers a, b, c
62+
// and return that pointer
63+
int* MedianOfThree(int* a, int* b, int* c)
64+
{
65+
if (*a < *b && *b < *c)
66+
return (b);
67+
68+
if (*a < *c && *c <= *b)
69+
return (c);
70+
71+
if (*b <= *a && *a < *c)
72+
return (a);
73+
74+
if (*b < *c && *c <= *a)
75+
return (c);
76+
77+
if (*c <= *a && *a < *b)
78+
return (a);
79+
80+
if (*c <= *b && *b <= *a)
81+
return (b);
82+
}
83+
84+
// A Utility function to perform intro sort
85+
void IntrosortUtil(int arr[], int* begin, int* end,
86+
int depthLimit)
87+
{
88+
int size = end - begin;
89+
if (size < 16) {
90+
InsertionSort(arr, begin, end);
91+
return;
92+
}
93+
94+
// If the depth is zero use heapsort
95+
if (depthLimit == 0) {
96+
make_heap(begin, end + 1);
97+
sort_heap(begin, end + 1);
98+
return;
99+
}
100+
101+
int* pivot
102+
= MedianOfThree(begin, begin + size / 2, end);
103+
104+
// Swap the values pointed by the two pointers
105+
swapValue(pivot, end);
106+
107+
// Perform Quick Sort
108+
int* partitionPoint
109+
= Partition(arr, begin - arr, end - arr);
110+
IntrosortUtil(arr, begin, partitionPoint - 1,
111+
depthLimit - 1);
112+
IntrosortUtil(arr, partitionPoint + 1, end,
113+
depthLimit - 1);
114+
115+
return;
116+
}
117+
118+
/* Implementation of introsort*/
119+
void Introsort(int arr[], int* begin, int* end)
120+
{
121+
int depthLimit = 2 * log(end - begin);
122+
123+
// Perform a recursive Introsort
124+
IntrosortUtil(arr, begin, end, depthLimit);
125+
126+
return;
127+
}
128+
129+
130+
void printArray(int arr[], int n)
131+
{
132+
for (int i = 0; i < n; i++)
133+
cout << arr[i] << " \n"[i + 1 == n];
134+
}
135+
136+
// Driver program
137+
int main()
138+
{
139+
int arr[] = {10, 84, 97, 97, 16, 86, 94, 46, 19, 42, 33, 45, 36, 6, 1 };
140+
int n = sizeof(arr) / sizeof(arr[0]);
141+
Introsort(arr, arr, arr + n - 1);
142+
printArray(arr, n);
143+
144+
return (0);
145+
}

0 commit comments

Comments
 (0)