Advertisement
Guest User

Untitled

a guest
Apr 5th, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5.  
  6.  
  7.  
  8. //------------------------------------------------------------------
  9. // Initialize data array with random values between 0 and range-1
  10. //------------------------------------------------------------------
  11. void random_data(int data[], int count, int range)
  12. {
  13. // Put specified count of random numbers into data array
  14. for (int index = 0; index < count; index++)
  15. data[index] = rand() % range;
  16. }
  17.  
  18.  
  19.  
  20.  
  21. //-----------------------------------------------------------------
  22. // Bubble sort algorithm -- modified
  23. //-----------------------------------------------------------------
  24. void bubble_sort(int data[], int count)
  25. {
  26. int pass = 1;
  27. int pass2 = 1;
  28. int exchange = 1;
  29.  
  30. // Bubble largest value to the right N times
  31. while ((pass < count) && (exchange > 0))
  32. {
  33. // Scan unsorted part of data array
  34. exchange = 0;
  35. for (int index = 0; index < count - pass; index++)
  36. {
  37. // Swap two data values if out of order
  38. if (data[index] > data[index + 1])
  39. {
  40. int temp = data[index];
  41. data[index] = data[index + 1];
  42. data[index + 1] = temp;
  43. exchange++;
  44.  
  45. }
  46. cout << "index1 : " << index << endl;
  47. }
  48. pass++;
  49.  
  50. for(int i = 0; i < count - pass2; i++)
  51. {
  52. if(data[count - i] < data[count - i - 1])
  53. { int temp2 = data[i];
  54. data[count - i] = data[count - i - 1];
  55. data[count - i - 1] = temp2;
  56. exchange ++;
  57. }
  58.  
  59.  
  60. cout << "index2 : " << count-i << endl; }
  61. }
  62. pass2++;
  63.  
  64.  
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. int main()
  73. {
  74. //Create Array of 100 random integer values ranging from 0 to 999
  75. int SIZE = 100;
  76. int array[SIZE];
  77. int range = 1000;
  78.  
  79. random_data(array, SIZE, range);
  80.  
  81. //Test & view what the random function did
  82.  
  83. //for(int i = 0; i <SIZE; i++)
  84. //{
  85. // cout << "Value at " << i << ": " << array[i] << endl;
  86. //}
  87.  
  88. //Test & view if it is sorted by modified bubble
  89. bubble_sort(array, SIZE);
  90.  
  91. //for(int i = 0; i <SIZE; i++)
  92. //{
  93. // cout << "Value at " << i << ": " << array[i] << endl;
  94. //}
  95.  
  96.  
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement