









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










docsity.com
Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them This puts the largest element at the very end The last element is now in the correct and final place
Compare each element (except the last two ) with its neighbor to the right If they are out of order, swap them This puts the second largest element next to last The last two elements are now in their correct and final places
Compare each element (except the last three ) with its neighbor to the right Continue as above until you have no unsorted elements on the left
docsity.com
public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--) { // counting down for (inner = 0; inner < outer; inner++) { // bubbling up if (a[inner] > a[inner + 1]) { // if out of order... int temp = a[inner]; // ...then swap a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } }
docsity.com
for (outer = a.length - 1; outer > 0; outer--) { for (inner = 0; inner < outer; inner++) { if (a[inner] > a[inner + 1]) { // code for swap omitted } } }
Let n = a.length = size of the array
The outer loop is executed n-1 times (call it n, that’s close enough)
Each time the outer loop is executed, the inner loop is executed
Inner loop executes n-1 times at first, linearly dropping to just once On average, inner loop executes about n/2 times for each execution of the outer loop In the inner loop, the comparison is always done (constant time), the swap might be done (also constant time)
Result is n * n/2 * k, that is, O(n2/2 + k) = O(n^2 )
docsity.com
Given an array of length n,
Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 Search elements 2 through n-1 and select the smallest Swap it with the element in location 2 Search elements 3 through n-1 and select the smallest Swap it with the element in location 3 Continue in this fashion until there’s nothing left to search
docsity.com
Example and analysis of selection sort
The selection sort might swap an array element with itself--this is harmless, and not worth checking for
Analysis: The outer loop executes n-1 times The inner loop executes about n/ times on average (from n to 2 times) Work done in the inner loop is constant (swap two array elements) Time required is roughly (n-1)*(n/2) You should recognize this as O(n^2 )
docsity.com
For the inner loop:
This loop searches through the array, incrementing inner from its initial value of outer+1 up to a.length- As the loop proceeds, min is set to the index of the smallest number found so far Our invariant is: for all i such that outer <= i <= inner, a[min] <= a[i]
For the outer (enclosing) loop:
The loop counts up from outer = 0 Each time through the loop, the minimum remaining value is put in a[outer] Our invariant is: for all i <= outer, if i < j then a[i] <= a[j]
docsity.com
The outer loop of insertion sort is:
for (outer = 1; outer < a.length; outer++) {...}
The invariant is that all the elements to the left of outer
are sorted with respect to one another For all i < outer, j < outer, if i < j then a[i] <= a[j] This does not mean they are all in their final correct place; the remaining array elements may need to be inserted When we increase outer, a[outer-1] becomes to its left; we must keep the invariant true by inserting a[outer-1] into its proper place This means: Finding the element’s proper place Making room for the inserted element (by shifting over other elements) Inserting the element docsity.com
We run once through the outer loop, inserting each of n
elements; this is a factor of n
On average, there are n/2 elements already sorted
The inner loop looks at (and moves) half of these This gives a second factor of n/
Hence, the time required for an insertion sort of an array
of n elements is proportional to n^2 /
Discarding constants, we find that insertion sort is O(n^2 )
docsity.com
Split the array into two or more parts
Sort each part individually
(^4 7 12 14 20 21 24 30 9 10 16 23 28 33 38 )
Merge
docsity.com
Very roughly, here’s how to sort large amounts of data:
Repeat: Read in as much data as fits in memory Sort it, using a fast sorting algorithm (quicksort may be a good choice) Write out the sorted data to a new file After all the data has been written into smaller, individually sorted files: Read in the initial portion of each sorted file into individual arrays Start merging the arrays Whenever an array becomes empty, read in more data from its file Every so often, write the destination array to the (one) final output file
When you are done, you will have one (large) sorted file
docsity.com 16
Most of the sorting techniques we have discussed are O(n^2 )
As we will see later, we can do much better than this with somewhat more complicated sorting algorithms
Within O(n^2 ), Bubble sort is very slow, and should probably never be used for anything Selection sort is intermediate in speed Insertion sort is usually faster than selection sort—in fact, for small arrays (say, 10 or 20 elements), insertion sort is faster than more complicated sorting algorithms Merge sort, if done in memory, is O(n log n)
Selection sort and insertion sort are “good enough” for small arrays
Merge sort is good for sorting data that doesn’t fit in main memory
docsity.com