Skip to content

Commit b44c7d4

Browse files
committed
Final
1 parent 0bb7e56 commit b44c7d4

11 files changed

+49
-19
lines changed
File renamed without changes.

221-12c-A2-code/bubble-sort.cpp final/bubble-sort.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ BubbleSort::sort(int A[], int size) // main entry point
1616
while(!done)
1717
{
1818
done = true;
19-
num_cmps ++;
20-
for(int n = 0; n < size ; ++n)
19+
for(int n = 1; n < size ; ++n)
2120
{
22-
num_cmps++;
21+
num_cmps += 2;
2322
if(A[n] < A[n-1])
2423
{
2524
int temp = A[n];
File renamed without changes.

221-12c-A2-code/insertion-sort.cpp final/insertion-sort.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ InsertionSort::sort(int A[], int size) // main entry point
1515
int j=0;
1616
int temp=0;
1717
int num=0;
18+
num_cmps++;
1819
for (int i=1; i<size; ++i)
1920
{
2021
temp= A[i];
2122
j = i-1;
23+
num_cmps+=3;
2224
for(num=0; (j >= 0) && (A[j] > temp); num+=2)
2325
{
2426
A[j+1] = A[j];
2527
j -= 1;
28+
num_cmps+=2;
2629
}
2730
A[j+1]=temp;
28-
num_cmps+=num;
2931
}
3032
}
3133

File renamed without changes.
File renamed without changes.

221-12c-A2-code/radix-sort.cpp final/radix-sort.cpp

+25-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
#include "sort.h"
1010
#include <iostream>
11-
11+
#include <cmath>
12+
#include <ostream>
1213
void
1314
RadixSort::sort(int A[], int size)
1415
{
@@ -18,38 +19,55 @@ for(int i =0; i<size; ++i)//Find largest number
1819
{
1920
if(A[i]>digits)
2021
digits=A[i];
22+
2123
}
2224
digits=log10(digits);//find highest digit number
25+
std::cout<<"Largest digit found\n";
2326
int B[10];
2427
int C[size];
2528
int D[size];
26-
pow=1;
27-
while(true)//Get all the digits in the array
29+
int pow=1;
30+
int temp=0;
31+
32+
while(log10(pow)<7)//Get all the digits in the array
2833
{
34+
for(int a=0; a<size; a++)
35+
{
36+
C[a]=0;
37+
D[a]=0;
38+
}
39+
for(int b=0; b<10; b++)
40+
{
41+
B[b]=0;
42+
}
2943
for(int j=0; j<size; ++j)//Set digits
3044
{
3145
D[j]=(A[j]/(pow))%10;
3246
}
33-
3447
//Counting sort starts here
3548
for(int k=0; k<size; ++k)//Set number of each digit.
3649
{
37-
B[D[k]]++;
50+
++B[D[k]];
3851
}
52+
if (B[0]==size)
53+
{
54+
return;}
3955
for(int l=1; l<10; ++l)//Do the less than equal part.
4056
{
41-
B[l]=B[l]+B[l-1];
57+
B[l]=B[l]+B[l-1];
4258
}
59+
int fix=size-1;
4360
for(int m=size-1; m>=0; --m)//Actual Sorting Here
4461
{
4562
C[B[D[m]]-1]=A[m];
4663
--B[D[m]];
4764
}
65+
4866
for(int n=0; n<size; ++n)//reset A with new sorted part
4967
{
5068
A[n]=C[n];
5169
}
52-
if(digits=pow/10)//Detects if done with digits
70+
if(digits==pow)//Detects if done with digits
5371
break;
5472
pow*=10;
5573
}

221-12c-A2-code/selection-sort.cpp final/selection-sort.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@
88

99
#include "sort.h"
1010
#include <iostream>
11-
11+
using namespace std;
1212
void
1313
SelectionSort::sort(int A[], int size) // main entry point
1414
{
15+
cout << "Selection Sort!" << endl;
1516
num_cmps = 0;
1617
int min = 0;
1718
int temp = 0;
18-
for(int n = 1; n < size; ++n)
19+
for(int n = 0; n < size; ++n)
1920
{
2021
num_cmps++;
21-
min = n-1;
22-
for(int j = n; j < size; ++n)
22+
min = n;
23+
for(int j = n + 1; j < size; ++j)
2324
{
24-
num_cmps++;
25+
num_cmps += 2;
2526
if(A[j] < A[min])
2627
min = j;
2728
}
@@ -30,4 +31,5 @@ SelectionSort::sort(int A[], int size) // main entry point
3031
A[n] = A[min];
3132
A[min] = temp;
3233
}
34+
num_cmps++;
3335
}

221-12c-A2-code/shell-sort.cpp final/shell-sort.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,32 @@
88

99
#include "sort.h"
1010
#include <iostream>
11+
using namespace std;
1112

1213
void
1314
ShellSort::sort(int A[], int size)
1415
{
16+
cout << "shell sort!" << endl;
1517
int temp = 0;
1618
num_cmps = 0;
1719
int step = size/2;
1820
while(step > 0)
1921
{
22+
++num_cmps;
2023
for(int i = step; i < size; ++i)
2124
{
22-
temp = A[i];
23-
for(int j = i; j >= step && A[j-step]; j -= step)
25+
++num_cmps;
26+
for(int j = i; j >= step && A[j] < A[j-step]; j -= step)
2427
{
28+
num_cmps += 2;
29+
temp = A[j];
2530
A[j] = A[j-step];
2631
A[j - step] = temp;
2732
}
33+
num_cmps += 2;
2834
}
35+
++num_cmps;
2936
step /= 2;
3037
}
38+
++num_cmps;
3139
}

221-12c-A2-code/sort.cpp final/sort.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int main(int argc, char** argv)
103103
}
104104

105105
/* read number of integers */
106+
cout << "Enter Size >>>";
106107
int size; //number of integers
107108
if (!(cin >> size)) return 1; //exit abnormally
108109

221-12c-A2-code/sort.h final/sort.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class Sort {
1313
protected:
14-
int num_cmps; // number of comparisons performed in sort function
14+
long int num_cmps; // number of comparisons performed in sort function
1515
public:
1616
virtual void sort(int A[], int size) = 0; // main entry point
1717
bool testIfSorted(int A[], int size); // returns false if not sorted

0 commit comments

Comments
 (0)