Skip to content

Instantly share code, notes, and snippets.

@neizod
Last active August 29, 2015 14:06
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 neizod/69dc54166a3dc614de03 to your computer and use it in GitHub Desktop.
Save neizod/69dc54166a3dc614de03 to your computer and use it in GitHub Desktop.
Sorting Algorithms
#include <iostream>
using namespace std;
int arr[] = {2,5,3,9,1,0,7,4,8,6};
void show(int i=-1) {
for (int k=0; k<10; k++) {
if (i-1 == k) cout << "\x1b[37;1m";
if (i == k) cout << "\x1b[31;1m";
cout << arr[k];
if (i == k) cout << "\x1b[0m";
}
cout << endl;
}
int main(void) {
show();
bool swapped = false;
do {
swapped = false;
for (int i=1; i<10; i++) {
if (arr[i-1] > arr[i]) {
arr[i] ^= arr[i-1] ^= arr[i] ^= arr[i-1];
swapped = true;
show(i);
}
}
} while (swapped);
show();
return 0;
}
#include <iostream>
using namespace std;
int arr[] = {2,5,3,9,1,0,7,4,8,6};
void show(int i=-1, int j=10) {
for (int k=0; k<10; k++) {
if (i >= k) cout << "\x1b[37;44m";
if (j == k) cout << "\x1b[31;1m";
if (j+1 == k) cout << "\x1b[37;1m";
cout << arr[k];
if (i >= k) cout << "\x1b[0m";
}
cout << endl;
}
int main(void) {
show();
for (int i=0; i<10; i++) {
int j = i;
while (j > 0 && arr[j-1] > arr[j]) {
arr[j] ^= arr[j-1] ^= arr[j] ^= arr[j-1];
j -= 1;
show(i, j);
}
}
show();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment