Created
March 17, 2017 09:25
-
-
Save gpetuhov/9302f989771b00788ab1c37a54d1baeb to your computer and use it in GitHub Desktop.
C++ Bubble sort array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int n = 10; //number of elements | |
int arr[n]; //array of n elements | |
int i,j; //array indexes | |
int temp; //temporary variable for sorting | |
srand(rand()); | |
cout << "Unsorted array:" << endl; | |
for (i = 0; i < n; i++) { | |
arr[i] = rand(); | |
cout << arr[i] << " "; | |
} | |
cout << endl; | |
for (i = 0; i < n-1; i++) { | |
for (j = i+1; j < n; j++) { | |
if (arr[i] > arr[j]) { | |
temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
} | |
cout << "Sorted array:" << endl; | |
for (i = 0; i < n; i++) { | |
cout << arr[i] << " "; | |
} | |
cout << endl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment