Skip to content

Commit 957a68e

Browse files
authored
Add files via upload
1 parent 2935775 commit 957a68e

File tree

4 files changed

+309
-0
lines changed

4 files changed

+309
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <iostream>
2+
#include <cmath>
3+
4+
using namespace std;
5+
6+
template <class T>
7+
void Print(T& vec, int n, string s){
8+
cout << s << ": [" << flush;
9+
for (int i=0; i<n; i++){
10+
cout << vec[i] << flush;
11+
if (i < n-1){
12+
cout << ", " << flush;
13+
}
14+
}
15+
cout << "]" << endl;
16+
}
17+
18+
int Max(int A[], int n){
19+
int max = -32768;
20+
for (int i=0; i<n; i++){
21+
if (A[i] > max){
22+
max = A[i];
23+
}
24+
}
25+
return max;
26+
}
27+
28+
// Linked List node
29+
class Node{
30+
public:
31+
int value;
32+
Node* next;
33+
};
34+
35+
int countDigits(int x){
36+
int count = 0;
37+
while (x != 0){
38+
x = x / 10;
39+
count++;
40+
}
41+
return count;
42+
}
43+
44+
void initializeBins(Node** p, int n){
45+
for (int i=0; i<n; i++){
46+
p[i] = nullptr;
47+
}
48+
}
49+
50+
void Insert(Node** ptrBins, int value, int idx){
51+
Node* temp = new Node;
52+
temp->value = value;
53+
temp->next = nullptr;
54+
55+
if (ptrBins[idx] == nullptr){
56+
ptrBins[idx] = temp; // ptrBins[idx] is head ptr
57+
} else {
58+
Node* p = ptrBins[idx];
59+
while (p->next != nullptr){
60+
p = p->next;
61+
}
62+
p->next = temp;
63+
}
64+
}
65+
66+
int Delete(Node** ptrBins, int idx){
67+
Node* p = ptrBins[idx]; // ptrBins[idx] is head ptr
68+
ptrBins[idx] = ptrBins[idx]->next;
69+
int x = p->value;
70+
delete p;
71+
return x;
72+
}
73+
74+
int getBinIndex(int x, int idx){
75+
return (int)(x / pow(10, idx)) % 10;
76+
}
77+
78+
void RadixSort(int A[], int n){
79+
int max = Max(A, n);
80+
int nPass = countDigits(max);
81+
82+
// Create bins array
83+
Node** bins = new Node* [10];
84+
85+
// Initialize bins array with nullptr
86+
initializeBins(bins, 10);
87+
88+
// Update bins and A for nPass times
89+
for (int pass=0; pass<nPass; pass++){
90+
91+
// Update bins based on A values
92+
for (int i=0; i<n; i++){
93+
int binIdx = getBinIndex(A[i], pass);
94+
Insert(bins, A[i], binIdx);
95+
}
96+
97+
// Update A with sorted elements from bin
98+
int i = 0;
99+
int j = 0;
100+
while (i < 10){
101+
while (bins[i] != nullptr){
102+
A[j++] = Delete(bins, i);
103+
}
104+
i++;
105+
}
106+
// Initialize bins with nullptr again
107+
initializeBins(bins, 10);
108+
}
109+
110+
// Delete heap memory
111+
delete [] bins;
112+
}
113+
114+
int main() {
115+
116+
int A[] = {237, 146, 259, 348, 152, 163, 235, 48, 36, 62};
117+
int n = sizeof(A)/sizeof(A[0]);
118+
119+
Print(A, n, "\t\tA");
120+
RadixSort(A, n);
121+
Print(A, n, " Sorted A");
122+
123+
return 0;
124+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
template <class T>
6+
void Print(T& vec, int n, string s){
7+
cout << s << ": [" << flush;
8+
for (int i=0; i<n; i++){
9+
cout << vec[i] << flush;
10+
if (i < n-1){
11+
cout << ", " << flush;
12+
}
13+
}
14+
cout << "]" << endl;
15+
}
16+
17+
int Max(int A[], int n){
18+
int max = -32768;
19+
for (int i=0; i<n; i++){
20+
if (A[i] > max){
21+
max = A[i];
22+
}
23+
}
24+
return max;
25+
}
26+
27+
// Linked List node
28+
class Node{
29+
public:
30+
int value;
31+
Node* next;
32+
};
33+
34+
void Insert(Node** ptrBins, int idx){
35+
Node* temp = new Node;
36+
temp->value = idx;
37+
temp->next = nullptr;
38+
39+
if (ptrBins[idx] == nullptr){ // ptrBins[idx] is head ptr
40+
ptrBins[idx] = temp;
41+
} else {
42+
Node* p = ptrBins[idx];
43+
while (p->next != nullptr){
44+
p = p->next;
45+
}
46+
p->next = temp;
47+
}
48+
}
49+
50+
int Delete(Node** ptrBins, int idx){
51+
Node* p = ptrBins[idx]; // ptrBins[idx] is head ptr
52+
ptrBins[idx] = ptrBins[idx]->next;
53+
int x = p->value;
54+
delete p;
55+
return x;
56+
}
57+
58+
void BinSort(int A[], int n){
59+
int max = Max(A, n);
60+
61+
// Create bins array
62+
Node** bins = new Node* [max + 1];
63+
64+
// Initialize bins array with nullptr
65+
for (int i=0; i<max+1; i++){
66+
bins[i] = nullptr;
67+
}
68+
69+
// Update count array values based on A values
70+
for (int i=0; i<n; i++){
71+
Insert(bins, A[i]);
72+
}
73+
74+
// Update A with sorted elements
75+
int i = 0;
76+
int j = 0;
77+
while (i < max+1){
78+
while (bins[i] != nullptr){
79+
A[j++] = Delete(bins, i);
80+
}
81+
i++;
82+
}
83+
84+
// Delete heap memory
85+
delete [] bins;
86+
}
87+
88+
int main() {
89+
90+
int A[] = {2, 5, 8, 12, 3, 6, 7, 10};
91+
int n = sizeof(A)/sizeof(A[0]);
92+
93+
Print(A, n, "\t\tA");
94+
BinSort(A, n);
95+
Print(A, n, " Sorted A");
96+
cout << endl;
97+
return 0;
98+
}

Sorting Technique/17 Shellsort.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Shell Sort
2+
#include <stdio.h>
3+
#include<stdlib.h>
4+
void swap(int *x,int *y)
5+
{
6+
int temp=*x;
7+
*x=*y;
8+
*y=temp;
9+
}
10+
void ShellSort(int A[],int n)
11+
{
12+
int gap,i,j,temp;
13+
14+
for(gap=n/2;gap>=1;gap/=2)
15+
{
16+
for(i=gap;i<n;i++)
17+
{
18+
temp=A[i];
19+
j=i-gap;
20+
while(j>=0 && A[j]>temp)
21+
{
22+
A[j+gap]=A[j];
23+
j=j-gap;
24+
}
25+
A[j+gap]=temp;
26+
27+
}
28+
}
29+
30+
}
31+
int main()
32+
{
33+
int A[]={11,13,7,12,16,9,24,5,10,3},n=10,i;
34+
35+
SellSort(A,n);
36+
37+
for(i=0;i<10;i++)
38+
printf("%d ",A[i]);
39+
printf("\n");
40+
41+
return 0;
42+
}

Sorting Technique/18 ShellSort.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
3+
using std::cout;
4+
using std::endl;
5+
using std::flush;
6+
using std::string;
7+
8+
template <class T>
9+
void Print(T& vec, int n, string s){
10+
cout << s << ": [" << flush;
11+
for (int i=0; i<n; i++){
12+
cout << vec[i] << flush;
13+
if (i < n-1){
14+
cout << ", " << flush;
15+
}
16+
}
17+
cout << "]" << endl;
18+
}
19+
20+
// Code is similar to Insertion Sort with some modifications
21+
void ShellSort(int A[], int n){
22+
for (int gap=n/2; gap>=1; gap/=2){
23+
for (int j=gap; j<n; j++){
24+
int temp = A[j];
25+
int i = j - gap;
26+
while (i >= 0 && A[i] > temp){
27+
A[i+gap] = A[i];
28+
i = i-gap;
29+
}
30+
A[i+gap] = temp;
31+
}
32+
}
33+
}
34+
35+
int main() {
36+
37+
int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3};
38+
int n = sizeof(A)/sizeof(A[0]);
39+
40+
Print(A, n, "\t\tA");
41+
ShellSort(A, n);
42+
Print(A, n, " Sorted A");
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)