Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60f95da

Browse files
Merge pull request #42 from navdeepkk/master
Created heapSort.cpp
2 parents 3876364 + cc4d899 commit 60f95da

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

‎sort_search_problems/SleepSort.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//This is the most simple implementation of sleeping sort in c++//
2+
#include<iostream>
3+
#include<thread>
4+
#include<vector>
5+
#include <chrono>
6+
using namespace std;
7+
8+
void sleepSort(int*, int);
9+
void sleep(int);
10+
11+
int main()
12+
{
13+
//initialize an array of integers to be sorted
14+
// Doesn't work for negative numbers
15+
int arr[] = { 3,2,1 };
16+
int n = sizeof(arr) / sizeof(arr[0]);
17+
18+
//find the max element in arr
19+
//Why to ding max? Read below
20+
int max = arr[0];
21+
for (int i = 0; i < n; i++) {
22+
if (arr[i] > max) max = arr[i];
23+
}
24+
max++;
25+
26+
//call the function SLEEPSORT()
27+
sleepSort(arr, n);
28+
29+
//make the main thread to go to sleep for 1 second more then
30+
//the largest number in the list.
31+
//this ensures all the threads are over before main finishes execution.
32+
std::this_thread::sleep_for(std::chrono::seconds(max));
33+
34+
35+
return(0);
36+
}
37+
38+
void sleep( int i) {
39+
//make the thread to sleep for I seconds.
40+
std::this_thread::sleep_for(std::chrono::seconds(i));
41+
cout << i << " ";
42+
}
43+
44+
void sleepSort(int* arr, int n) {
45+
thread* threads;
46+
threads = new thread[n];
47+
for (int i = 0; i < n; i++) {
48+
//a new thread is created for each element of an array, which goes to sleep for arr[i] seconds.
49+
threads[i] = (thread(sleep, arr[i]));
50+
}
51+
}

‎sort_search_problems/heapSort.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
5+
void maxHeapify(int*, int, int);
6+
void buildHeap(int*, int);
7+
void heapsort(int*, int);
8+
9+
int main() {
10+
11+
int arr[] = { 4,2,3,1,5,6,8,};
12+
13+
heapsort(arr, sizeof(arr) / sizeof(int));
14+
15+
for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
16+
cout << arr[i] << " ";
17+
}
18+
19+
return 0;
20+
}
21+
22+
void maxHeapify(int* arr, int n, int i) {
23+
if (i > n / 2 - 1) {
24+
//if index of element is greater than i/2 it is in the last level of the heap. no need to call maxHeapify for it.
25+
return;
26+
}
27+
if (arr[i] > arr[2 * i] && arr[i] > arr[(2 * i) + 1]) {
28+
//heap property already satisfied.
29+
return;
30+
}
31+
32+
int maxInx = i, temp;
33+
34+
while (i <= (n / 2 - 1)) {
35+
//finding the index of the largest child of i.
36+
if (arr[2 * i + 1] > arr[i] && (2 * i + 1) < n) {
37+
maxInx = 2 * i + 1;
38+
}
39+
if (arr[(2 * i) + 2] > arr[maxInx] && (2 * i + 2) < n) {
40+
maxInx = (2 * i) + 2;
41+
}
42+
//if no change occurs. then return.
43+
if (i == maxInx) { return; }
44+
45+
//having obtained the index of the largest node swap it with i.
46+
//swapping arr[i] with arr[maxInx]
47+
temp = arr[maxInx];
48+
arr[maxInx] = arr[i];
49+
arr[i] = temp;
50+
i = maxInx;
51+
}
52+
53+
}
54+
55+
void buildHeap(int* arr, int n) {
56+
//start calling from the last parent in the heap(n/2) and go upto the root node.
57+
//start from the last parent instead of root because here the loop invacriant is that heap condition is
58+
//satisfied for all nodes other than the one for which it is called. which would not be true when it is
59+
//called for the root node of the array when the array is compeletely random.
60+
//refer to CORMEN for more details.
61+
for (int i = (n / 2 - 1); i >= 0; i--) {
62+
maxHeapify(arr, n, i);
63+
}
64+
}
65+
66+
void heapsort(int* arr, int n) {
67+
buildHeap(arr, n);
68+
69+
int temp;
70+
while (n > 0) {
71+
//after the heap is built take the element a[0], swap it with the last element, discard it and call maxheapify
72+
//for the root element
73+
74+
temp = arr[n - 1];
75+
arr[n - 1] = arr[0];
76+
arr[0] = temp;
77+
n--;
78+
maxHeapify(arr, n, 0);
79+
}
80+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /