// Joe Gillotti - 10/24/2011 // in like 10 minutes /* joe@adore:~/programming/c/bubblesort$ ./a.out Before sorting: listing these nums: 23 #0 - 2 #1 - 4 #2 - 23 #3 - 6 #4 - 7 #5 - 23 #6 - 5 #7 - 4 #8 - 3 #9 - 2 #10 - 5 #11 - 23 #12 - 4 #13 - 5 #14 - 7 #15 - 7 #16 - 3 #17 - 4 #18 - 5345 #19 - 6 #20 - 54 #21 - 2 #22 - 34 Stopping sort after 21 passes After sorting: listing these nums: 23 #0 - 5345 #1 - 54 #2 - 34 #3 - 23 #4 - 23 #5 - 23 #6 - 7 #7 - 7 #8 - 7 #9 - 6 #10 - 6 #11 - 5 #12 - 5 #13 - 5 #14 - 4 #15 - 4 #16 - 4 #17 - 4 #18 - 3 #19 - 3 #20 - 2 #21 - 2 #22 - 2 joe@adore:~/programming/c/bubblesort$ */ #include #include // List the stuff in our array, in the current order inline void listShit(int* shit, const int num); int main() { // Shitty unsorted list int shit[] = {2,4,23,6,7,23,5,4,3,2,5,23,4,5,7,7,3,4,5345, 6, 54,2,34}; // Number of them unsigned const int num = sizeof (shit)/sizeof(int); // Our fucking iterator unsigned int i; // Tmp for sorting int tmp; // Gotta know when to stop bool done = false; // How many passes did it fucking tkae? int passes = 1; // List before; unsorted, untouched printf("Before sorting:\n"); listShit(shit, num); // Go until we're positive we're successful while (!done) { // Start off optimistic done = true; // Go over each element in the array for (i = 0; i < num; ++i) { // If this isn't the last item in the list and the // one right after it is bigger if (i + 1 != num && shit[i] < shit[i+1]) { // Set temp to old current value tmp = shit[i]; // Set current value to the next value, which // is higher shit[i] = shit[i+1]; // Set next value to current value that's lower shit[i+1] = tmp; // if we do this at least once, we'll need to // go back to correct again done = false; } } // If nothing fucked up (as in done wasn't set to // false again), the loop won't run again and we're likely done if (done) printf("Stopping sort after %d passes\n", passes); ++passes; } // List after; touched printf("\n\nAfter sorting:\n"); listShit(shit, num); return 0; } inline void listShit(int* shit, const int num) { unsigned int i; printf("listing these nums: %d\n", num); for (i = 0; i < num; ++i) printf("#%d - %d\n", i, shit[i]); }