Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void sortList(int [], int); // function prototype
- int main()
- {
- const int MAX = 4;
- int list[MAX] = {22,5,67,98};
- int i, moves;
- moves = sortList(list, MAX);
- cout << "The sorted list, in ascending order, is:\n";
- for (i = 0; i < MAX; ++i)
- cout << " " <<list[i];
- cout << endl << moves << " moves were made to sort this list\n";
- return 0;
- }
- void sortList(int list[], int max)
- {
- int i, j, temp, moves = 0;
- for ( i = 0; i < (max - 1); i++)
- {
- for(j = 1; j < max; j++)
- {
- if (list[j] < list[j-1])
- {
- temp = list[j];
- list[j] = list[j-1];
- list[j-1] = temp;
- moves++;
- }
- }
- }
- return moves;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement