Advertisement
Guest User

Selection

a guest
Nov 13th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. /* Bubble is a bubble sorting algorithem made by Naren Ram on Novemeber 13 */
  2.  
  3. #include <stdio.h>
  4. #include <cs50.h>
  5.  
  6. int main(void)
  7. {
  8.     int ints[16] ={12,4,54,3,29,17,90,1,7,9,22,35,41,78,3,59};// unsorted arry
  9.     int n = 15; // length of arry
  10.     for(int i =0; i<=n;i++) // iterate though arry
  11.     {
  12.         int temp; // hold value for switch
  13.         int lowest = ints[i]; // define temperary lowest
  14.         int min_index; // define teperary lowest numbers' index
  15.         int j;
  16.         bool switchMade; //was a switch made
  17.         int doSwitch = 0; // should i do a switch now
  18.         for(j=i+1;j<=n;j++) // iterate though new arry
  19.         {
  20.             if(ints[j]<lowest) // condition to determine if new lower
  21.             {
  22.                 lowest = ints[j]; // define new lower
  23.                 min_index = j; // define new index
  24.                 switchMade = true; // a switch was made
  25.                 doSwitch = 1; // so we need to switch
  26.             }
  27.             if(doSwitch==0) // no switch was made
  28.             {
  29.                 switchMade = false; // do not switch
  30.             }
  31.         }
  32.         if(switchMade) // if we did switch do the switch
  33.         {
  34.             temp = ints[i];
  35.             ints[i]= lowest;
  36.             ints[min_index] = temp;
  37.         }
  38.     }
  39.     for(int i =0;i<=n;i++) // print arry
  40.     {
  41.         printf("%i\n",ints[i]);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement