Skip to content

Commit c6ff179

Browse files
Sorting Algorithms in C++
1 parent 0eb52ec commit c6ff179

15 files changed

+1297
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//AddressCalculationSort.cpp : Program of sorting using address calculation sort.
2+
3+
#include<iostream>
4+
using namespace std;
5+
6+
static const int maxSize = 30;
7+
8+
class Node
9+
{
10+
public:
11+
int info;
12+
Node *link;
13+
Node(int data)
14+
{
15+
info = data;
16+
link = NULL;
17+
}
18+
};//End of class Node
19+
20+
class SortedLinkedList
21+
{
22+
private:
23+
Node *start;
24+
bool isEmpty() const;
25+
public:
26+
SortedLinkedList();
27+
~SortedLinkedList();
28+
void insert(int data);
29+
Node* getStart();
30+
};//End of class SortedLinkedList
31+
32+
SortedLinkedList::SortedLinkedList()
33+
{
34+
start = NULL;
35+
}//End of SortedLinkedList()
36+
37+
SortedLinkedList::~SortedLinkedList()
38+
{
39+
Node *ptr;
40+
41+
while(start != NULL)
42+
{
43+
ptr = start->link;
44+
delete start;
45+
start = ptr;
46+
}
47+
}//End of ~SortedLinkedList()
48+
49+
Node* SortedLinkedList::getStart()
50+
{
51+
return start;
52+
}//End of getStart()
53+
54+
inline bool SortedLinkedList::isEmpty() const
55+
{
56+
return start == NULL;
57+
}//End of isEmpty()
58+
59+
void SortedLinkedList::insert(int data)
60+
{
61+
Node *temp = new Node(data);
62+
63+
//List empty or new node to be inserted before first node
64+
if(isEmpty() || data < start->info)
65+
{
66+
temp->link = start;
67+
start = temp;
68+
}
69+
else
70+
{
71+
Node *ptr = start;
72+
while(ptr->link != NULL && ptr->link->info < data)
73+
ptr = ptr->link;
74+
temp->link = ptr->link;
75+
ptr->link = temp;
76+
}
77+
}//End of insert()
78+
79+
int hashFn(int x, int large)
80+
{
81+
float temp;
82+
temp = (float)x / large;
83+
return (int)(temp * 5);
84+
}//End of hashFn()
85+
86+
void addressCalculationSort(int arr[], int n)
87+
{
88+
SortedLinkedList list[6];
89+
int i;
90+
91+
int large = 0;
92+
for(i=0; i<n; i++)
93+
{
94+
if(arr[i] > large)
95+
large = arr[i];
96+
}
97+
98+
int x;
99+
for(i=0; i<n; i++)
100+
{
101+
x = hashFn(arr[i],large);
102+
list[x].insert(arr[i]);
103+
}
104+
105+
//Elements of linked lists are copied to array
106+
Node *ptr;
107+
i = 0;
108+
for(int j=0; j<=5; j++)
109+
{
110+
ptr = list[j].getStart();
111+
while(ptr != NULL)
112+
{
113+
arr[i++] = ptr->info;
114+
ptr = ptr->link;
115+
}
116+
}//End of for
117+
}//End of addressCalculationSort()
118+
119+
int main()
120+
{
121+
int arr[maxSize] = {194, 289, 566, 432, 654, 98, 232, 415, 345, 276, 532, 254, 165, 965, 476};
122+
int n = 15;
123+
124+
cout << "Unsorted list is :\n";
125+
for(int i=0; i<n; i++)
126+
cout << arr[i] << " ";
127+
cout << "\n";
128+
129+
addressCalculationSort(arr,n);
130+
131+
cout << "Sorted list is :\n";
132+
for(int i=0; i<n; i++)
133+
cout << arr[i] << " ";
134+
cout << "\n";
135+
136+
}//End of main()

sorting-algorithms/BinaryTreeSort.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//BinaryTreeSort.cpp : Program of sorting using binary tree sort.
2+
3+
#include<iostream>
4+
using namespace std;
5+
6+
static const int maxSize = 30;
7+
8+
class Node
9+
{
10+
public:
11+
int info;
12+
Node *lchild;
13+
Node *rchild;
14+
15+
Node(int data)
16+
{
17+
info = data;
18+
lchild = NULL;
19+
rchild = NULL;
20+
}
21+
};//End of class Node
22+
23+
class BinarySearchTree
24+
{
25+
private:
26+
Node *root;
27+
static int k;
28+
29+
private:
30+
void destroy(Node *ptr);
31+
Node* insert(Node *ptr, int data);
32+
void inorder(Node *ptr, int arr[]);
33+
public:
34+
BinarySearchTree();
35+
~BinarySearchTree();
36+
bool isEmpty();
37+
void insert(int data);
38+
void inorder(int arr[]);
39+
};//End of class BinarySearchTree
40+
41+
int BinarySearchTree::k;
42+
43+
BinarySearchTree::BinarySearchTree()
44+
{
45+
root = NULL;
46+
}//End of constructor BinarySearchTree()
47+
48+
BinarySearchTree::~BinarySearchTree()
49+
{
50+
destroy(root);
51+
root = NULL;
52+
}//End of destructor ~BinarySearchTree()
53+
54+
void BinarySearchTree::destroy(Node *ptr)
55+
{
56+
if(ptr == NULL)
57+
return;
58+
destroy(ptr->lchild);
59+
destroy(ptr->rchild);
60+
delete ptr;
61+
}//End of destroy()
62+
63+
bool BinarySearchTree::isEmpty()
64+
{
65+
return root == NULL;
66+
}//End of isEmpty()
67+
68+
Node* BinarySearchTree::insert(Node *ptr, int data)
69+
{
70+
if(ptr == NULL)
71+
ptr = new Node(data);
72+
else if(data < ptr->info)
73+
ptr->lchild = insert(ptr->lchild, data);
74+
else
75+
ptr->rchild = insert(ptr->rchild, data);
76+
return ptr;
77+
}//End of insert()
78+
79+
void BinarySearchTree::insert(int data)
80+
{
81+
root = insert(root, data);
82+
}//End of insert()
83+
84+
//Recursive inorder traversal
85+
void BinarySearchTree::inorder(Node *ptr, int arr[])
86+
{
87+
if(ptr == NULL)
88+
return;
89+
inorder(ptr->lchild,arr);
90+
arr[k++] = ptr->info;
91+
inorder(ptr->rchild,arr);
92+
}//End of inorder()
93+
94+
void BinarySearchTree::inorder(int arr[])
95+
{
96+
k = 0;
97+
inorder(root,arr);
98+
}//End of inorder()
99+
100+
void binaryTreeSort(int arr[], int n)
101+
{
102+
BinarySearchTree bst;
103+
104+
for(int i = 0; i < n; i++)
105+
bst.insert(arr[i]);
106+
bst.inorder(arr);
107+
}//End of binaryTreeSort()
108+
109+
int main()
110+
{
111+
int arr[maxSize] = {19, 35, 10, 12, 46, 6, 40, 3, 90, 8};
112+
int n = 10;
113+
114+
cout << "Unsorted list is :\n";
115+
for(int i=0; i<n; i++)
116+
cout << arr[i] << " ";
117+
cout << "\n";
118+
119+
binaryTreeSort(arr,n);
120+
121+
cout << "Sorted list is :\n";
122+
for(int i=0; i<n; i++)
123+
cout << arr[i] << " ";
124+
cout << "\n";
125+
126+
}//End of main()
127+
128+
129+

sorting-algorithms/BubbleSort.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//BubbleSort.cpp : Program of sorting using bubble sort.
2+
3+
#include<iostream>
4+
using namespace std;
5+
6+
static const int maxSize = 30;
7+
8+
void bubbleSort(int arr[], int n)
9+
{
10+
int temp, xchanges;
11+
12+
for(int i=0; i<n-1 ;i++)
13+
{
14+
xchanges = 0;
15+
for(int j=0; j<n-1-i; j++)
16+
{
17+
if(arr[j] > arr[j+1])
18+
{
19+
temp = arr[j];
20+
arr[j] = arr[j+1];
21+
arr[j+1] = temp;
22+
xchanges++;
23+
}
24+
}
25+
if(xchanges == 0) //If list is sorted
26+
break;
27+
}//End of for
28+
}//End of bubbleSort()
29+
30+
int main()
31+
{
32+
int arr[maxSize] = {40, 20, 50, 60, 30, 10, 90, 97, 70, 80};
33+
int n = 10;
34+
35+
cout << "Unsorted list is :\n";
36+
for(int i=0; i<n; i++)
37+
cout << arr[i] << " ";
38+
cout << "\n";
39+
40+
bubbleSort(arr,n);
41+
42+
cout << "Sorted list is :\n";
43+
for(int i=0; i<n; i++)
44+
cout << arr[i] << " ";
45+
cout << "\n";
46+
47+
}//End of main()
48+

0 commit comments

Comments
 (0)