Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #define DATA_SIZE 32
- // Sorting bubble
- int bulbSort(int *Array, int size)
- {
- int iter, jter;
- int x ;
- for (iter=1; iter<size; iter++)
- {
- for (jter=size-1; jter>=iter; jter--)
- {
- if (Array[jter]<Array[jter-1])
- {
- x = Array[jter-1] ;
- Array[jter-1] = Array[jter] ;
- Array[jter] = x;
- }
- }
- }
- return *Array;
- }
- // Sorting by choice
- int choicesSort(int* arrayPtr, int length_array)
- {
- for (int repeat_counter = 0; repeat_counter < length_array; repeat_counter++)
- {
- int temp = arrayPtr[0]; // Temporary change to store the permutation value
- for (int element_counter = repeat_counter + 1; element_counter < length_array; element_counter++)
- {
- if (arrayPtr[repeat_counter] > arrayPtr[element_counter])
- {
- temp = arrayPtr[repeat_counter];
- arrayPtr[repeat_counter] = arrayPtr[element_counter];
- arrayPtr[element_counter] = temp;
- }
- }
- }
- return *arrayPtr;
- }
- //Output of sorted values
- void print(void * data, int count/* 0 - full DATA_SIZE*/)
- {
- int index = 0;
- std::cout<< "Your sort algoritm: ";
- for (index = 0; (!count || index < count) && index < DATA_SIZE; index++){
- printf("%d ", ((int *)data)[index]);
- }
- printf("\n");
- }
- int main(void)
- {
- int data[DATA_SIZE] = { 1, 23, 39, 28, 10, 26, 25, 24,
- 293, 221, 21, 350, 19, 18, 17, 163,
- 81, 144, 13, 12, 11, 43, 9, 8,
- 7, 6, 5, 4, 30, 2, 0, 581 };
- //Determining how long the functions run
- auto begin1 = std::chrono::high_resolution_clock::now();
- bulbSort(data, DATA_SIZE-1);
- auto end1 = std::chrono::high_resolution_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end1-begin1).count();
- std::cout <<"Algoritm \"sortbulb\" " << duration << " mcs" << std::endl;
- print(data, 0);
- auto begin2 = std::chrono::high_resolution_clock::now();
- choicesSort(data,DATA_SIZE-1);
- auto end2 = std::chrono::high_resolution_clock::now();
- auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end2-begin2).count();
- std::cout <<"Algoritm \"sortcoice\" "<< duration2 << " mcs" << std::endl;
- print(data, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment