Skip to content

Commit 4af329c

Browse files
authoredAug 31, 2020
Add files
0 parents  commit 4af329c

File tree

3 files changed

+236
-0
lines changed

3 files changed

+236
-0
lines changed
 

‎Analysis.pdf

134 KB
Binary file not shown.

‎Readme.pdf

223 KB
Binary file not shown.

‎Sorting.cpp

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
#include<iostream>
2+
#include<cstdlib> // used for random number generation
3+
#include<math.h> // used for ceil and floor functions in adaptive sort
4+
#include<ctime> // used for measuring time
5+
using namespace std;
6+
7+
class sortElements {
8+
protected:
9+
int numElements; //Number of elements of the array
10+
int* elements; //dynamic array with the elements of type int
11+
public:
12+
sortElements(); // default constructor
13+
sortElements(int n); // non-default constructor
14+
~sortElements() {}; // destructor
15+
void generateRandom(int seed, int lower, int upper); // will generate
16+
//numElement set of elements randomly with the seedand range from lower to upper
17+
18+
void displayElements(int* arr); // display the given set
19+
int* getElementsArray(); // return the entire array of elements
20+
int getnumElements(); // return the number of elements
21+
int* bubblesort(int* arr, int low, int high); //bubblesort method
22+
int* quicksort(int* arr, int low, int high);//quicksort method
23+
int* shellsort(int* arr, int low, int high);//quicksort method
24+
int partition(int* arr, int low, int high); //to help quicksort method inorder for pivot values
25+
void swap(int* orginal, int* previous); //swap places method
26+
};
27+
28+
sortElements::sortElements()
29+
{
30+
numElements = 0; //initiate
31+
elements = NULL;
32+
}
33+
34+
sortElements::sortElements(int n)
35+
{
36+
numElements = n; //non-default
37+
elements = new int[n];
38+
}
39+
40+
void sortElements::generateRandom(int seed, int lower, int upper)
41+
{
42+
srand(seed); //create a seed
43+
int range = upper - lower + 1; //set the range
44+
for (int i = 0; i < numElements; i++)
45+
{
46+
elements[i] = rand() % range + lower; //generate random numbers
47+
48+
}
49+
}
50+
51+
void sortElements::displayElements(int* arr)
52+
{
53+
for (int i = 0; i < numElements; i++)
54+
{
55+
cout << arr[i] << " "; //display elements
56+
}
57+
cout << endl;
58+
}
59+
60+
int* sortElements::getElementsArray()
61+
{
62+
return elements;
63+
}
64+
65+
int sortElements::getnumElements()
66+
{
67+
return numElements;
68+
}
69+
70+
int* sortElements::bubblesort(int* arr, int low, int high)
71+
{
72+
73+
int i, j;
74+
bool is_swap;
75+
for (i = 0; i < numElements - 1; i++) //outer loop
76+
{
77+
is_swap = false;
78+
for (j = 0; j < numElements - i - 1; j++) //inner loop
79+
{
80+
if (arr[j] > arr[j + 1]) //check which element is greater
81+
{
82+
swap(&arr[j], &arr[j + 1]);
83+
is_swap = true;
84+
}
85+
}
86+
if (is_swap == false) //if boolean value is false when no elements are swapped
87+
break;
88+
}
89+
int* bubblesort = new int[numElements];
90+
for (int k = 0; k < numElements; k++) {
91+
bubblesort[k] = elements[k]; // to go through a loop for numberofelements given
92+
}
93+
//delete [] bubblesort; //to free up memory
94+
return bubblesort;
95+
}
96+
97+
int* sortElements::quicksort(int* arr, int low, int high)
98+
{
99+
if (low < high)
100+
{
101+
102+
int index_part = partition(arr, low, high); //call partition method
103+
quicksort(arr, low, index_part - 1); ///sort elements before partition recurssive call
104+
quicksort(arr, index_part + 1, high); //sorted elements after partition recurssive call
105+
}
106+
int* quicksort = new int[numElements];
107+
for (int k = 0; k < numElements; k++) {
108+
quicksort[k] = elements[k]; // to go through a loop for numberofelements given
109+
}
110+
//delete [] quicksort; //to free up memory
111+
return quicksort;
112+
113+
114+
}
115+
116+
int* sortElements::shellsort(int* arr, int low, int high)
117+
{
118+
// Start with a big gap, then reduce the gap
119+
for (int gap = numElements / 2; gap > 0; gap /= 2)
120+
{
121+
// Do a gapped insertion sort for this gap size.
122+
// The first gap elements a[0..gap-1] are already in gapped order
123+
// keep adding one more element until the entire array is
124+
// gap sorted
125+
for (int i = gap; i < numElements; i += 1)
126+
{
127+
// add a[i] to the elements that have been gap sorted
128+
// save a[i] in temp and make a hole at position i
129+
int temp = arr[i];
130+
131+
// shift earlier gap-sorted elements up until the correct
132+
// location for a[i] is found
133+
int j;
134+
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
135+
arr[j] = arr[j - gap];
136+
137+
// put temp (the original a[i]) in its correct location
138+
arr[j] = temp;
139+
}
140+
}
141+
int* shellsort = new int[numElements];
142+
for (int k = 0; k < numElements; k++) {
143+
shellsort[k] = elements[k]; // to go through a loop for numberofelements given
144+
}
145+
//delete [] shellsort; //to free up memory
146+
return shellsort;
147+
}
148+
149+
150+
int sortElements::partition(int* arr, int low, int high)
151+
{
152+
int pivot = arr[high]; //set pivot
153+
int i = (low - 1); // index of least element
154+
155+
for (int j = low; j <= high - 1; j++)
156+
{
157+
158+
if (arr[j] < pivot) // compare current element with the pivot
159+
{
160+
i++; // increase index of lower element
161+
swap(&arr[i], &arr[j]); //call the swap method
162+
}
163+
}
164+
swap(&arr[i + 1], &arr[high]); //incremented value swap
165+
return (i + 1);
166+
}
167+
168+
169+
void sortElements::swap(int* orginal, int* previous)
170+
{
171+
int temp = *orginal; //store in temporary value
172+
*orginal = *previous;
173+
*previous = temp;
174+
}
175+
176+
int main() {
177+
int numElements,seed{},lower{},upper{},n; //to store the input from input text file
178+
cin >> numElements;
179+
sortElements* sort = new sortElements(numElements); //pass number of elements
180+
cin.get(); //read line
181+
while (!cin.eof()) {
182+
cin >> seed >> lower >> upper;
183+
184+
};
185+
n = numElements;
186+
cout << "Number of elements: " << numElements << endl;
187+
sort->generateRandom(seed,lower,upper); //call genearte random method with given values
188+
cout << "Pseudorandom elements:" << endl;
189+
sort->displayElements(sort->getElementsArray()); //before any change actaul values generated
190+
cout << "Bubble Sort:" << endl;
191+
sort->displayElements(sort->bubblesort(sort->getElementsArray(), 0, n - 1)); //result from bubblesort
192+
cout << "Quick Sort:" << endl;
193+
sort->displayElements(sort->quicksort(sort->getElementsArray(), 0, n - 1)); //result from quicksort
194+
cout << "Shell Sort:" << endl;
195+
sort->displayElements(sort->shellsort(sort->getElementsArray(), 0, n - 1)); //result from shellsort
196+
cout << "Adaptive Sort:" << endl;
197+
sort->displayElements(sort->shellsort(sort->getElementsArray(), 0, n - 1)); //result from adptivesort
198+
199+
/*
200+
//to calculate execution time//
201+
clock_t start, stop; // clock variables to get the time
202+
double totalTime; //to store bubblesort time
203+
double totalTime1; //to store quicksort time
204+
double totalTime2; //to store shellsort time
205+
//for bubblesort
206+
start = clock(); //clock to check the start time
207+
sort->bubblesort(sort->getElementsArray(), 0, n - 1);
208+
stop = clock();//clock to check the End time
209+
totalTime = ((double)(stop - start)) / CLOCKS_PER_SEC;//calculating the total time
210+
cout << "time taken for bubblesort " << totalTime << endl; //displaying the total time taken
211+
212+
//for quicksort
213+
start = clock(); //clock to check the start time
214+
sort->quicksort(sort->getElementsArray(), 0, n - 1);
215+
stop = clock();//clock to check the End time
216+
totalTime1 = ((double)(stop - start)) / CLOCKS_PER_SEC;//calculating the total time
217+
cout << "time taken for quicksort " << totalTime1 << endl; //displaying the total time taken
218+
219+
//for shellsort
220+
start = clock(); //clock to check the start time
221+
sort->shellsort(sort->getElementsArray(), 0, n - 1);
222+
stop = clock();//clock to check the End time
223+
totalTime2 = ((double)(stop - start)) / CLOCKS_PER_SEC;//calculating the total time
224+
cout << "time taken for shellsort " << totalTime2 << endl; //displaying the total time taken*/
225+
226+
return 0;
227+
228+
}
229+
230+
/*
231+
references
232+
for ctime library-http://cplusplus.com/forum/beginner/146620/
233+
for bubblesort-https://www.geeksforgeeks.org/bubble-sort/
234+
for quicksort-https://www.geeksforgeeks.org/quick-sort/
235+
for shellsort-https://www.geeksforgeeks.org/shellsort/
236+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.