1. Bubblesort
1. compare 1st and 2nd elements
2. if 1st larger than 2nd, swap
3. compare 2nd and 3rd, and swap if necessary
4. continue until compare the last two elements
5. the largest element is now the last element in the array.
6. repeat statring from the beginning until no swaps are performed (i.e., the array is sorted)
7. each time you go through the elements bubbling up the largest element
8. no need to try the last i elements for the ith run since the end elements are already sorted
2. Selection Sort
1. array to be sorted: A
2. array to be returned: B
3. find smallest element in A and put in B
4. mark space in A with null so it won’t be chosen again
5. repeat last two steps until B is sorted array
3. Insertion Sort
1. algorithm passes through each element everything before element is sorted puts element in appropriate place in sorted half of array by checking each element starting from the back of the sorted part of the array
2. Code Methods: insertionsort
3. Worst Case O(N2) – when array is reverse sorted
4. Best Case O(N) – when array is already sorted
5. Swap Number
= number of inversions (i.e., i < j but a[i] > a[j])
Running Time = O(I + N)
Notice that for a sorted array I = 0
For a reverse array I = O(N2)
Average Number of inversions in an array of N distinct elements is N *
(N – 1 ) / 4 = O(N2)
6. Average Case O(n2)
7. Any algorithm that sorts by exchanging adjacent elements requires
O(N2 time on average.
Including Bubblesort and Selection Sort
4. Shellsort
1. invented by Donald Shell
2. Algorithm start separation = number of elements / 2 compare a[i] and a[i+separation] swap if necessary do for each element of array i = 0 to a.length – separation set separation = separation /2 recompare all the elements
2. separation calculation shell suggested /2 other sequences give great improvement note figure 7.3 does not / 2 properly
3. Code Methods: shellsort
4. Average