nick43ui

Untitled

Feb 26th, 2021 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3.  
  4. #define DATA_SIZE 32
  5.  
  6. // Sorting bubble
  7. int bulbSort(int *Array, int size)
  8. {
  9. int iter, jter;
  10. int x ;
  11. for (iter=1; iter<size; iter++)
  12. {
  13. for (jter=size-1; jter>=iter; jter--)
  14. {
  15. if (Array[jter]<Array[jter-1])
  16. {
  17. x = Array[jter-1] ;
  18. Array[jter-1] = Array[jter] ;
  19. Array[jter] = x;
  20. }
  21. }
  22. }
  23. return *Array;
  24. }
  25.  
  26. // Sorting by choice
  27. int choicesSort(int* arrayPtr, int length_array)
  28. {
  29. for (int repeat_counter = 0; repeat_counter < length_array; repeat_counter++)
  30. {
  31. int temp = arrayPtr[0]; // Temporary change to store the permutation value
  32. for (int element_counter = repeat_counter + 1; element_counter < length_array; element_counter++)
  33. {
  34. if (arrayPtr[repeat_counter] > arrayPtr[element_counter])
  35. {
  36. temp = arrayPtr[repeat_counter];
  37. arrayPtr[repeat_counter] = arrayPtr[element_counter];
  38. arrayPtr[element_counter] = temp;
  39. }
  40. }
  41. }
  42. return *arrayPtr;
  43. }
  44.  
  45. //Output of sorted values
  46. void print(void * data, int count/* 0 - full DATA_SIZE*/)
  47. {
  48. int index = 0;
  49. std::cout<< "Your sort algoritm: ";
  50. for (index = 0; (!count || index < count) && index < DATA_SIZE; index++){
  51. printf("%d ", ((int *)data)[index]);
  52. }
  53. printf("\n");
  54. }
  55. int main(void)
  56. {
  57. int data[DATA_SIZE] = { 1, 23, 39, 28, 10, 26, 25, 24,
  58. 293, 221, 21, 350, 19, 18, 17, 163,
  59. 81, 144, 13, 12, 11, 43, 9, 8,
  60. 7, 6, 5, 4, 30, 2, 0, 581 };
  61.  
  62.  
  63.  
  64. //Determining how long the functions run
  65. auto begin1 = std::chrono::high_resolution_clock::now();
  66. bulbSort(data, DATA_SIZE-1);
  67. auto end1 = std::chrono::high_resolution_clock::now();
  68. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end1-begin1).count();
  69. std::cout <<"Algoritm \"sortbulb\" " << duration << " mcs" << std::endl;
  70. print(data, 0);
  71.  
  72. auto begin2 = std::chrono::high_resolution_clock::now();
  73. choicesSort(data,DATA_SIZE-1);
  74. auto end2 = std::chrono::high_resolution_clock::now();
  75. auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end2-begin2).count();
  76. std::cout <<"Algoritm \"sortcoice\" "<< duration2 << " mcs" << std::endl;
  77. print(data, 0);
  78.  
  79.  
  80. return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment