Sorting


Work with your assigned partner to implement this sorting algorithm for arrays of integers:

while the array is unsorted
   for each pair
      if left > right
         swap left and right

Example output:

int[] nums = {8,2,10,4,-1};
sort(nums);

//prints out -1 2 4 8 10
for(int num : nums)
{
   System.out.printf("%d ", num); 
}


Walk Through



Challenge

Implement the algorithm to use Comparable instead of ints.

Selection Sort

Implement selection sort:

for i in 0 ... length - 1
   find the smallest value between i and the end (length -1)
   swap array[i] and smallest

Insertion Sort

Implement insertion sort:

for i in 0 ... length - 1
   for the current value, find the position to the left of i, called j where 
   the current value should go (i.e. array[j-1] <= current < array[j+1])

   swap array[i] and array[j]

This page was last modified on 2026-10-23 at 05:18:02.

Copyright © 2018–2026 George Fox University. All rights reserved.