Bubble Sort

  • Sorts a list of values by comparing the first and second values then sorting them, then comparing the second and third, then third and fourth, etc.
  • After doing this the largest number will be at the end of the list. Start over again at the beginning of the list and do this until you reach the n-1 values in the list. Continue this until the list is sorted.
  • Efficiency decreases with larger number of items
public class TurtleSort {
    public static void main(String[] args) {
        int[] turtleHeights = { 5, 2, 8, 1, 9 };

        // Print the original order of turtle heights
        System.out.println("Original Order:");
        for (int height : turtleHeights) {
            System.out.print(height + " ");
        }
        System.out.println();

        // Perform bubble sort on the array
        for (int i = 0; i < turtleHeights.length - 1; i++) {
            for (int j = 0; j < turtleHeights.length - i - 1; j++) {
                if (turtleHeights[j] > turtleHeights[j + 1]) {
                    int temp = turtleHeights[j];
                    turtleHeights[j] = turtleHeights[j + 1];
                    turtleHeights[j + 1] = temp;
                }
            }
        }

        // Print the sorted order of turtle heights
        System.out.println("Sorted Order:");
        for (int height : turtleHeights) {
            System.out.print(height + " ");
        }
    }
}
TurtleSort.main(null);
Original Order:
5 2 8 1 9 
Sorted Order:
1 2 5 8 9 

Selection Sort

  • Choosing the smallest number and swapping
  • Can implement with Linked List
  • You set a value as the minimum, iterate through the array, and any value you find that smaller than the one you set becomes the minimum. You swap the minimum with the original value.
public class TurtleSort2 {
    public static void main(String[] args) {
        int[] turtleHeights = { 5, 2, 8, 1, 9 };

        // Print the original order of turtle heights
        System.out.println("Original Order:");
        for (int height : turtleHeights) {
            System.out.print(height + " ");
        }
        System.out.println();

        // Perform selection sort on the array
        for (int i = 0; i < turtleHeights.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < turtleHeights.length; j++) {
                if (turtleHeights[j] < turtleHeights[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = turtleHeights[i];
            turtleHeights[i] = turtleHeights[minIndex];
            turtleHeights[minIndex] = temp;
        }

        // Print the sorted order of turtle heights
        System.out.println("Sorted Order:");
        for (int height : turtleHeights) {
            System.out.print(height + " ");
        }
    }
}
TurtleSort2.main(null);
Original Order:
5 2 8 1 9 
Sorted Order:
1 2 5 8 9 

Insertion Sort

  • starts with an i and j
  • trying to determine where the value goes in front of it
  • i stays at 0, and j goes along cross dimensional
  • makes insertion slot for number
  • Insertion sort iterates, consuming one input element each repetition, and grows a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
public class TurtleSort3 {
    public static void main(String[] args) {
        int[] turtleHeights = { 5, 2, 8, 1, 9 };

        // Print the original order of turtle heights
        System.out.println("Original Order:");
        for (int height : turtleHeights) {
            System.out.print(height + " ");
        }
        System.out.println();

        // Perform insertion sort on the array
        for (int i = 1; i < turtleHeights.length; i++) {
            int key = turtleHeights[i];
            int j = i - 1;
            while (j >= 0 && turtleHeights[j] > key) {
                turtleHeights[j + 1] = turtleHeights[j];
                j--;
            }
            turtleHeights[j + 1] = key;
        }

        // Print the sorted order of turtle heights
        System.out.println("Sorted Order:");
        for (int height : turtleHeights) {
            System.out.print(height + " ");
        }
    }
}
TurtleSort3.main(null);
Original Order:
5 2 8 1 9 
Sorted Order:
1 2 5 8 9