// APS101, Winter 2009: Lecture 32 (Mar. 30) // // Review: last time we finished working on our Tic-Tac-Toe game, and thought about writing a transpose method for 2-D matrices. // (see Matrix.java) Matrix.transpose(new int[][]{{1,2,3},{4,5,6}, {7,8,9}}) Matrix.transpose({{1,2,3},{4,5,6}, {7,8,9}}) // doesn't work! // now, we will consider the problem of "sorting" a list of integers. // sorting: putting a series of elements in a particular order. (ex. ascending order - from lowest to highest) // 3 basic sorts: 1) bubble sort 2) insertion sort 3) selection sort // (these are inefficient for large lists with many elements) // there are faster sorting algorithms - like Quicksort and Mergesort - // but we won't cover them in this course. Bubblesort: At each round a "bubble" is moving and swapping each pair if they're not in the correct order, so technically the max value of that round is moving towards the end. // (see Sort.java) Sort.bubbleSort(new int[]{1, 2, 3, 4, 5}) Sort.bubbleSort(new int[]{5, 4, 3, 2, 1}) // now let's write a more efficient version of bubblesort... Sort.bubbleSort2(new int[]{1, 2, 3, 4, 5}) Sort.bubbleSort2(new int[]{5, 4, 3, 2, 1}) // notice the change in the number of comparisons and swaps!