Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Bubble is a bubble sorting algorithem made by Naren Ram on Novemeber 13 */
- #include <stdio.h>
- #include <cs50.h>
- int main(void)
- {
- int ints[16] ={12,4,54,3,29,17,90,1,7,9,22,35,41,78,3,59};// unsorted arry
- int n = 15; // length of arry
- for(int i =0; i<=n;i++) // iterate though arry
- {
- int temp; // hold value for switch
- int lowest = ints[i]; // define temperary lowest
- int min_index; // define teperary lowest numbers' index
- int j;
- bool switchMade; //was a switch made
- int doSwitch = 0; // should i do a switch now
- for(j=i+1;j<=n;j++) // iterate though new arry
- {
- if(ints[j]<lowest) // condition to determine if new lower
- {
- lowest = ints[j]; // define new lower
- min_index = j; // define new index
- switchMade = true; // a switch was made
- doSwitch = 1; // so we need to switch
- }
- if(doSwitch==0) // no switch was made
- {
- switchMade = false; // do not switch
- }
- }
- if(switchMade) // if we did switch do the switch
- {
- temp = ints[i];
- ints[i]= lowest;
- ints[min_index] = temp;
- }
- }
- for(int i =0;i<=n;i++) // print arry
- {
- printf("%i\n",ints[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement