Skip to content

Commit 5071cd1

Browse files
authored
Added new: Quick Sort Algo (#70)
* Added new: Quick Sort Algo * Added new: Binary Search Tree Algo
1 parent b167cc9 commit 5071cd1

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed

Diff for: C++/binarySearchTree.cpp

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
struct Node {
5+
int data;
6+
Node *left;
7+
Node *right;
8+
};
9+
10+
struct Node* create(int item)
11+
{
12+
struct Node* node = new Node;
13+
node->data = item;
14+
node->left = node->right = NULL;
15+
return node;
16+
}
17+
18+
void search(struct Node* &cur, int item, struct Node* &parent)
19+
{
20+
while (cur != NULL && cur->data != item)
21+
{
22+
parent = cur;
23+
if (item < cur->data)
24+
cur = cur->left;
25+
else
26+
cur = cur->right;
27+
}
28+
}
29+
30+
struct Node* findMinimum(struct Node* cur)
31+
{
32+
while(cur->left != NULL) {
33+
cur = cur->left;
34+
}
35+
return cur;
36+
}
37+
38+
struct Node* findMaximum(struct Node* cur)
39+
{
40+
while(cur->right != NULL) {
41+
cur = cur->right;
42+
}
43+
return cur;
44+
}
45+
46+
struct Node* insertion(struct Node* root, int item)
47+
{
48+
if (root == NULL)
49+
return create(item); //return new node if tree is empty
50+
if (item < root->data)
51+
root->left = insertion(root->left, item);
52+
else
53+
root->right = insertion(root->right, item);
54+
return root;
55+
}
56+
void deletion(struct Node*& root, int item) //function to delete a struct Node/
57+
{
58+
struct Node* parent = NULL;
59+
struct Node* cur = root;
60+
search(cur, item, parent); //find the node to be deleted/
61+
if (cur == NULL)
62+
return;
63+
if (cur->left == NULL && cur->right == NULL) //When node has no children/
64+
{
65+
if (cur != root)
66+
{
67+
if (parent->left == cur)
68+
parent->left = NULL;
69+
else
70+
parent->right = NULL;
71+
}
72+
else
73+
root = NULL;
74+
free(cur);
75+
}
76+
else if (cur->left && cur->right)
77+
{
78+
struct Node* succ = findMinimum(cur->right);
79+
int val = succ->data;
80+
deletion(root, succ->data);
81+
cur->data = val;
82+
}
83+
else
84+
{
85+
struct Node* child = (cur->left)? cur->left: cur->right;
86+
if (cur != root)
87+
{
88+
if (cur == parent->left)
89+
parent->left = child;
90+
else
91+
parent->right = child;
92+
}
93+
else
94+
root = child;
95+
free(cur);
96+
}
97+
}
98+
99+
100+
101+
102+
103+
104+
int count = 0;
105+
struct Node* kthSmallest(struct Node* root, int k)
106+
{
107+
if (root == NULL)
108+
return NULL;
109+
110+
struct Node* left = kthSmallest(root->left, k);
111+
112+
if (left != NULL)
113+
return left;
114+
115+
count++;
116+
if (count == k)
117+
return root;
118+
119+
return kthSmallest(root->right, k);
120+
}
121+
122+
void printKthSmallest(struct Node* root, int k)
123+
{
124+
struct Node* res = kthSmallest(root, k);
125+
if (res == NULL)
126+
cout << "Not found";
127+
else
128+
cout << res->data;
129+
}
130+
131+
132+
int main(){
133+
struct Node* root = NULL;
134+
int choice,data;
135+
do{
136+
cin >> choice;
137+
switch(choice){
138+
case 1:
139+
cin >> data;
140+
root = insertion(root,data);
141+
break;
142+
case 2:
143+
cin >> data;
144+
deletion(root,data);
145+
break;
146+
case 3:
147+
cout << (findMinimum(root))->data << endl << (findMaximum(root))->data << endl;
148+
break;
149+
case 4:
150+
cin >> data;
151+
printKthSmallest(root,data);
152+
cout << endl;
153+
break;
154+
}
155+
}while(choice != 0);
156+
return 0;
157+
}

Diff for: C++/quicksort.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include<iostream>
2+
3+
using namespace std;
4+
5+
int partition(int A[], int l, int h)
6+
{
7+
int temp;
8+
int pivot = A[l];
9+
int i = l, j = h;
10+
do
11+
{
12+
do
13+
{
14+
i++;
15+
} while (A[i] >= pivot);
16+
do
17+
{
18+
j--;
19+
} while (A[j] < pivot);
20+
if (i < j){
21+
temp = A[i];
22+
A[i] = A[j];
23+
A[j] = temp;
24+
}
25+
} while (i < j);
26+
temp = A[l];
27+
A[l] = A[j];
28+
A[j] = temp;
29+
return j;
30+
}
31+
void QuickSort(int A[], int l, int h)
32+
{
33+
int j;
34+
if (l < h)
35+
{
36+
j = partition(A, l, h);
37+
QuickSort(A, l, j);
38+
QuickSort(A, j + 1, h);
39+
}
40+
}
41+
int main()
42+
{
43+
int size;
44+
cin >> size;
45+
int A[size];
46+
for(int i=0;i<size;i++){
47+
cin >> A[i];
48+
}
49+
QuickSort(A,0, size-1);
50+
for (int i = size-1; i >=0 ; i--)
51+
cout << A[i] << " ";
52+
}

0 commit comments

Comments
 (0)