Computer Science Module 1 Question 2(Sorting)

Zachary Kublalsingh

Unit2

Jul 12, 2024

Estimated reading time:


Contains all relevant information regarding sorting of lists, selection sort and bubble sort.




Selection Sort

Code

  
    #include
    int main()
    {
      int arr[10] = {20, 99, 48, 67, 19, 12, 93, 46, 18, 29} ; (can be any array)
      int i, j,min, temp;
        
      for( i = 0; i<9; i++)
      {
        min = i;
        for( j = i+1; i<10; j++)
        {
          if (arr[j] < arr[j+1])
          {
            min=j;
          }
          temp = arr[i];
          arr[i] = arr[min];
          arr[min] = temp;
        }
      }
    }
      
  

Bubble Sort

Code

  
    #include
    int main()
    {
      int arr[10] = {20, 99, 48, 67, 19, 12, 93, 46, 18, 29} ; (can be any array)
      int i, j,temp;
        
      for( i = 0; i<10; i++)
      {
        for( j = 0; i=10-i; j++)
        {
          if (arr[j] > arr[j+1])
          {
            temp = arr[j];
            arr[j] = arr[j+1];
            arr[j+1] = temp;
          }
        }
      }
    }       
  

Note - Sorting can be done in both ascending or descending order, the codes above are for ascending (smallest to largest)

⚠️ Did you spot an error? Contact Us!
Please include the title of the note, the respective Notebook and the error to speed up the process.

Read Next

Dont't forget to check out our Instagram Page!
edukatte_tt