Skip to content

Commit 0346894

Browse files
Merge pull request #123 from kooljais24/Kth_smallest_element
Added Find Kth smallest element in an unsorted array
2 parents 8e713a4 + 9945daf commit 0346894

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// A C++ program to find k'th smallest element using max heap
2+
#include<iostream>
3+
#include<climits>
4+
using namespace std;
5+
6+
// Prototype of a utility function to swap two integers
7+
void swap(int *x, int *y);
8+
9+
// A class for Max Heap
10+
class MaxHeap
11+
{
12+
int *harr; // pointer to array of elements in heap
13+
int capacity; // maximum possible size of max heap
14+
int heap_size; // Current number of elements in max heap
15+
public:
16+
MaxHeap(int a[], int size); // Constructor
17+
void maxHeapify(int i); //To maxHeapify subtree rooted with index i
18+
int parent(int i) { return (i-1)/2; }
19+
int left(int i) { return (2*i + 1); }
20+
int right(int i) { return (2*i + 2); }
21+
22+
int extractMax(); // extracts root (maximum) element
23+
int getMax() { return harr[0]; } // Returns maximum
24+
25+
// to replace root with new node x and heapify() new root
26+
void replaceMax(int x) { harr[0] = x; maxHeapify(0); }
27+
};
28+
29+
MaxHeap::MaxHeap(int a[], int size)
30+
{
31+
heap_size = size;
32+
harr = a; // store address of array
33+
int i = (heap_size - 1)/2;
34+
while (i >= 0)
35+
{
36+
maxHeapify(i);
37+
i--;
38+
}
39+
}
40+
41+
// Method to remove maximum element (or root) from max heap
42+
int MaxHeap::extractMax()
43+
{
44+
if (heap_size == 0)
45+
return INT_MAX;
46+
47+
// Store the maximum vakue.
48+
int root = harr[0];
49+
50+
// If there are more than 1 items, move the last item to root
51+
// and call heapify.
52+
if (heap_size > 1)
53+
{
54+
harr[0] = harr[heap_size-1];
55+
maxHeapify(0);
56+
}
57+
heap_size--;
58+
59+
return root;
60+
}
61+
62+
// A recursive method to heapify a subtree with root at given index
63+
// This method assumes that the subtrees are already heapified
64+
void MaxHeap::maxHeapify(int i)
65+
{
66+
int l = left(i);
67+
int r = right(i);
68+
int largest = i;
69+
if (l < heap_size && harr[l] > harr[i])
70+
largest = l;
71+
if (r < heap_size && harr[r] > harr[largest])
72+
largest = r;
73+
if (largest != i)
74+
{
75+
swap(&harr[i], &harr[largest]);
76+
maxHeapify(largest);
77+
}
78+
}
79+
80+
// A utility function to swap two elements
81+
void swap(int *x, int *y)
82+
{
83+
int temp = *x;
84+
*x = *y;
85+
*y = temp;
86+
}
87+
88+
// Function to return k'th largest element in a given array
89+
int kthSmallest(int arr[], int n, int k)
90+
{
91+
// Build a heap of first k elements: O(k) time
92+
MaxHeap mh(arr, k);
93+
94+
// Process remaining n-k elements. If current element is
95+
// smaller than root, replace root with current element
96+
for (int i=k; i<n; i++)
97+
if (arr[i] < mh.getMax())
98+
mh.replaceMax(arr[i]);
99+
100+
// Return root
101+
return mh.getMax();
102+
}
103+
104+
// Driver program to test above methods
105+
int main()
106+
{
107+
int arr[] = {12, 3, 5, 7, 19};
108+
int n = sizeof(arr)/sizeof(arr[0]), k = 4;
109+
cout << "K'th smallest element is " << kthSmallest(arr, n, k);
110+
return 0;
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// A C++ program to find k'th smallest element using min heap
2+
#include<iostream>
3+
#include<climits>
4+
using namespace std;
5+
6+
// Prototype of a utility function to swap two integers
7+
void swap(int *x, int *y);
8+
9+
// A class for Min Heap
10+
class MinHeap
11+
{
12+
int *harr; // pointer to array of elements in heap
13+
int capacity; // maximum possible size of min heap
14+
int heap_size; // Current number of elements in min heap
15+
public:
16+
MinHeap(int a[], int size); // Constructor
17+
void MinHeapify(int i); //To minheapify subtree rooted with index i
18+
int parent(int i) { return (i-1)/2; }
19+
int left(int i) { return (2*i + 1); }
20+
int right(int i) { return (2*i + 2); }
21+
22+
int extractMin(); // extracts root (minimum) element
23+
int getMin() { return harr[0]; } // Returns minimum
24+
};
25+
26+
MinHeap::MinHeap(int a[], int size)
27+
{
28+
heap_size = size;
29+
harr = a; // store address of array
30+
int i = (heap_size - 1)/2;
31+
while (i >= 0)
32+
{
33+
MinHeapify(i);
34+
i--;
35+
}
36+
}
37+
38+
// Method to remove minimum element (or root) from min heap
39+
int MinHeap::extractMin()
40+
{
41+
if (heap_size == 0)
42+
return INT_MAX;
43+
44+
// Store the minimum vakue.
45+
int root = harr[0];
46+
47+
// If there are more than 1 items, move the last item to root
48+
// and call heapify.
49+
if (heap_size > 1)
50+
{
51+
harr[0] = harr[heap_size-1];
52+
MinHeapify(0);
53+
}
54+
heap_size--;
55+
56+
return root;
57+
}
58+
59+
// A recursive method to heapify a subtree with root at given index
60+
// This method assumes that the subtrees are already heapified
61+
void MinHeap::MinHeapify(int i)
62+
{
63+
int l = left(i);
64+
int r = right(i);
65+
int smallest = i;
66+
if (l < heap_size && harr[l] < harr[i])
67+
smallest = l;
68+
if (r < heap_size && harr[r] < harr[smallest])
69+
smallest = r;
70+
if (smallest != i)
71+
{
72+
swap(&harr[i], &harr[smallest]);
73+
MinHeapify(smallest);
74+
}
75+
}
76+
77+
// A utility function to swap two elements
78+
void swap(int *x, int *y)
79+
{
80+
int temp = *x;
81+
*x = *y;
82+
*y = temp;
83+
}
84+
85+
// Function to return k'th smallest element in a given array
86+
int kthSmallest(int arr[], int n, int k)
87+
{
88+
// Build a heap of n elements: O(n) time
89+
MinHeap mh(arr, n);
90+
91+
// Do extract min (k-1) times
92+
for (int i=0; i<k-1; i++)
93+
mh.extractMin();
94+
95+
// Return root
96+
return mh.getMin();
97+
}
98+
99+
// Driver program to test above methods
100+
int main()
101+
{
102+
int arr[] = {12, 3, 5, 7, 19};
103+
int n = sizeof(arr)/sizeof(arr[0]), k = 2;
104+
cout << "K'th smallest element is " << kthSmallest(arr, n, k);
105+
return 0;
106+
}

0 commit comments

Comments
 (0)