Skip to content

Instantly share code, notes, and snippets.

@gpetuhov
Created March 17, 2017 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpetuhov/9302f989771b00788ab1c37a54d1baeb to your computer and use it in GitHub Desktop.
Save gpetuhov/9302f989771b00788ab1c37a54d1baeb to your computer and use it in GitHub Desktop.
C++ Bubble sort array
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