Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- //------------------------------------------------------------------
- // Initialize data array with random values between 0 and range-1
- //------------------------------------------------------------------
- void random_data(int data[], int count, int range)
- {
- // Put specified count of random numbers into data array
- for (int index = 0; index < count; index++)
- data[index] = rand() % range;
- }
- //-----------------------------------------------------------------
- // Bubble sort algorithm -- modified
- //-----------------------------------------------------------------
- void bubble_sort(int data[], int count)
- {
- int pass = 1;
- int pass2 = 1;
- int exchange = 1;
- // Bubble largest value to the right N times
- while ((pass < count) && (exchange > 0))
- {
- // Scan unsorted part of data array
- exchange = 0;
- for (int index = 0; index < count - pass; index++)
- {
- // Swap two data values if out of order
- if (data[index] > data[index + 1])
- {
- int temp = data[index];
- data[index] = data[index + 1];
- data[index + 1] = temp;
- exchange++;
- }
- cout << "index1 : " << index << endl;
- }
- pass++;
- for(int i = 0; i < count - pass2; i++)
- {
- if(data[count - i] < data[count - i - 1])
- { int temp2 = data[i];
- data[count - i] = data[count - i - 1];
- data[count - i - 1] = temp2;
- exchange ++;
- }
- cout << "index2 : " << count-i << endl; }
- }
- pass2++;
- }
- int main()
- {
- //Create Array of 100 random integer values ranging from 0 to 999
- int SIZE = 100;
- int array[SIZE];
- int range = 1000;
- random_data(array, SIZE, range);
- //Test & view what the random function did
- //for(int i = 0; i <SIZE; i++)
- //{
- // cout << "Value at " << i << ": " << array[i] << endl;
- //}
- //Test & view if it is sorted by modified bubble
- bubble_sort(array, SIZE);
- //for(int i = 0; i <SIZE; i++)
- //{
- // cout << "Value at " << i << ": " << array[i] << endl;
- //}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement