Skip to content

Commit f5a5d4a

Browse files
committed
added the num_cmps
1 parent 455bfb7 commit f5a5d4a

12 files changed

+709
-3
lines changed
6.67 KB
Binary file not shown.

bubble-sort.cpp

+1-2
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 ++;
2019
for(int n = 0; n < size ; ++n)
2120
{
22-
num_cmps++;
21+
num_cmps += 2;
2322
if(A[n] < A[n-1])
2423
{
2524
int temp = A[n];

insertion-sort - Copy - Copy.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//============================================================================
2+
// Name : insertion-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of insertion sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <ostream>
11+
12+
void
13+
InsertionSort::sort(int A[], int size) // main entry point
14+
{
15+
int j=0;
16+
int temp=0;
17+
int num=0;
18+
for (int i=1; i<size; ++i)
19+
{
20+
temp= A[i];
21+
j = i-1;
22+
for(num=0; (j >= 0) && (A[j] > temp); num+=2)
23+
{
24+
A[j+1] = A[j];
25+
j -= 1;
26+
}
27+
A[j+1]=temp;
28+
num_cmps+=num;
29+
}
30+
}
31+
32+
/*int i=0;
33+
int j=0;
34+
int key=0;
35+
{
36+
for (j=1; j<size; j++)
37+
{
38+
key= array[j];
39+
for (i=j-1; (i>=0) && (array[i]<key); i++)
40+
{
41+
array[i+1]= array[i];
42+
}
43+
array[i+1]=key;
44+
}
45+
return;
46+
}
47+
while(A[i]<A[i-1])
48+
{
49+
temp=A[i-1];
50+
A[i-temp] = A[i];
51+
A[i]=temp;
52+
}
53+
54+
55+
int temp=0;
56+
int j = 0;
57+
for(int i=1; i<size; ++i)
58+
{
59+
temp=A[i];
60+
for(j=i-1; j>=0 && A[j]<temp; ++j)
61+
A[j+1]=A[j];
62+
}
63+
A[j+1]=temp; */
64+

insertion-sort - Copy.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//============================================================================
2+
// Name : insertion-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of insertion sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <ostream>
11+
12+
void
13+
InsertionSort::sort(int A[], int size) // main entry point
14+
{
15+
int j=0;
16+
int temp=0;
17+
int num=0;
18+
for (int i=1; i<size; ++i)
19+
{
20+
temp= A[i];
21+
j = i-1;
22+
for(num=0; (j >= 0) && (A[j] > temp); num+=2)
23+
{
24+
A[j+1] = A[j];
25+
j -= 1;
26+
}
27+
A[j+1]=temp;
28+
num_cmps+=num;
29+
}
30+
}
31+
32+
/*int i=0;
33+
int j=0;
34+
int key=0;
35+
{
36+
for (j=1; j<size; j++)
37+
{
38+
key= array[j];
39+
for (i=j-1; (i>=0) && (array[i]<key); i++)
40+
{
41+
array[i+1]= array[i];
42+
}
43+
array[i+1]=key;
44+
}
45+
return;
46+
}
47+
while(A[i]<A[i-1])
48+
{
49+
temp=A[i-1];
50+
A[i-temp] = A[i];
51+
A[i]=temp;
52+
}
53+
54+
55+
int temp=0;
56+
int j = 0;
57+
for(int i=1; i<size; ++i)
58+
{
59+
temp=A[i];
60+
for(j=i-1; j>=0 && A[j]<temp; ++j)
61+
A[j+1]=A[j];
62+
}
63+
A[j+1]=temp; */
64+

radix-sort - Copy - Copy.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//============================================================================
2+
// Name : radix-sort.cpp
3+
// Author : Derek Wene
4+
// Date :
5+
// Copyright : Sure, lets say its copyrighted.
6+
// Description : Implementation of radix sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <iostream>
11+
#include <cmath>
12+
using namespace std;
13+
14+
void
15+
RadixSort::sort(int A[], int size)
16+
{
17+
int digits=0;
18+
int x=0;
19+
for(int i =0; i<size; ++i)//Find largest number
20+
{
21+
if(A[i]>digits)
22+
digits=A[i];
23+
}
24+
digits = floor(log(digits));//find highest digit number
25+
int B[10];
26+
int C[size];
27+
int D[size];
28+
int power=1;
29+
30+
while(power <= digits)//Get all the digits in the array
31+
{
32+
for(int n = 0; n < 10; ++n)
33+
{
34+
B[n] = 0;
35+
}
36+
for(int j=0; j<size; ++j)//Set digits
37+
{
38+
D[j] = (A[j]/((int)pow(10.0,power)))%10;
39+
}
40+
41+
//Counting sort starts here
42+
for(int k=0; k<size; ++k)//Set number of each digit.
43+
{
44+
B[D[k]]++;
45+
}
46+
int total = 0;
47+
for(int l=0; l<10; ++l)//Do the less than equal part.
48+
{
49+
int temp = B[l];
50+
B[l] = total;
51+
total += temp;
52+
}
53+
for(int m=0; m<size; ++m)//Actual Sorting Here
54+
{
55+
C[B[D[m]]] = A[m];
56+
++B[D[m]];
57+
}
58+
for(int n=0; n<size; ++n)//reset A with new sorted part
59+
{
60+
A[n] = C[n];
61+
C[n] = 0;
62+
D[n] = 0;
63+
}
64+
65+
++power;
66+
}
67+
68+
}

radix-sort - Copy.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//============================================================================
2+
// Name : radix-sort.cpp
3+
// Author : Derek Wene
4+
// Date :
5+
// Copyright : Sure, lets say its copyrighted.
6+
// Description : Implementation of radix sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <iostream>
11+
#include <cmath>
12+
using namespace std;
13+
14+
void
15+
RadixSort::sort(int A[], int size)
16+
{
17+
int digits=0;
18+
int x=0;
19+
for(int i =0; i<size; ++i)//Find largest number
20+
{
21+
if(A[i]>digits)
22+
digits=A[i];
23+
}
24+
digits = floor(log(digits));//find highest digit number
25+
int B[10];
26+
int C[size];
27+
int D[size];
28+
int power=1;
29+
30+
while(power <= digits)//Get all the digits in the array
31+
{
32+
for(int n = 0; n < 10; ++n)
33+
{
34+
B[n] = 0;
35+
}
36+
for(int j=0; j<size; ++j)//Set digits
37+
{
38+
D[j] = (A[j]/((int)pow(10.0,power)))%10;
39+
}
40+
41+
//Counting sort starts here
42+
for(int k=0; k<size; ++k)//Set number of each digit.
43+
{
44+
B[D[k]]++;
45+
}
46+
int total = 0;
47+
for(int l=0; l<10; ++l)//Do the less than equal part.
48+
{
49+
int temp = B[l];
50+
B[l] = total;
51+
total += temp;
52+
}
53+
for(int m=0; m<size; ++m)//Actual Sorting Here
54+
{
55+
C[B[D[m]]] = A[m];
56+
++B[D[m]];
57+
}
58+
for(int n=0; n<size; ++n)//reset A with new sorted part
59+
{
60+
A[n] = C[n];
61+
C[n] = 0;
62+
D[n] = 0;
63+
}
64+
65+
++power;
66+
}
67+
68+
}

selection-sort - Copy - Copy.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//============================================================================
2+
// Name : selection-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of selection sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <iostream>
11+
using namespace std;
12+
void
13+
SelectionSort::sort(int A[], int size) // main entry point
14+
{
15+
cout << "Selection Sort!" << endl;
16+
num_cmps = 0;
17+
int min = 0;
18+
int temp = 0;
19+
for(int n = 0; n < size; ++n)
20+
{
21+
num_cmps++;
22+
min = n;
23+
for(int j = n + 1; j < size; ++j)
24+
{
25+
num_cmps++;
26+
if(A[j] < A[min])
27+
min = j;
28+
}
29+
num_cmps++;
30+
temp = A[n];
31+
A[n] = A[min];
32+
A[min] = temp;
33+
}
34+
}

selection-sort - Copy.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//============================================================================
2+
// Name : selection-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of selection sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <iostream>
11+
using namespace std;
12+
void
13+
SelectionSort::sort(int A[], int size) // main entry point
14+
{
15+
cout << "Selection Sort!" << endl;
16+
num_cmps = 0;
17+
int min = 0;
18+
int temp = 0;
19+
for(int n = 0; n < size; ++n)
20+
{
21+
num_cmps++;
22+
min = n;
23+
for(int j = n + 1; j < size; ++j)
24+
{
25+
num_cmps++;
26+
if(A[j] < A[min])
27+
min = j;
28+
}
29+
num_cmps++;
30+
temp = A[n];
31+
A[n] = A[min];
32+
A[min] = temp;
33+
}
34+
}

selection-sort.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ SelectionSort::sort(int A[], int size) // main entry point
2222
min = n;
2323
for(int j = n + 1; j < size; ++j)
2424
{
25-
num_cmps++;
25+
num_cmps += 2;
2626
if(A[j] < A[min])
2727
min = j;
2828
}
@@ -31,4 +31,5 @@ SelectionSort::sort(int A[], int size) // main entry point
3131
A[n] = A[min];
3232
A[min] = temp;
3333
}
34+
num_cmps++;
3435
}

shell-sort.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ int temp = 0;
1919
int step = size/2;
2020
while(step > 0)
2121
{
22+
++num_cmps;
2223
for(int i = step; i < size; ++i)
2324
{
25+
++num_cmps;
2426
for(int j = i; j >= step && A[j] < A[j-step]; j -= step)
2527
{
28+
num_cmps += 2;
2629
temp = A[j];
2730
A[j] = A[j-step];
2831
A[j - step] = temp;
2932
}
33+
num_cmps += 2;
3034
}
35+
++num_cmps;
3136
step /= 2;
3237
}
38+
++num_cmps;
3339
}

0 commit comments

Comments
 (0)