100 likes | 215 Views
Array Sorting Algorithms. for(int I = 0; I < 10; i++) { System.out.println( “ I = “ +I); } No difference in for loop but there is a difference when using in an expression i++ - Use the current value of I in the expression then increment by 1. for(int I = 0; I < 10; ++i) {
E N D
for(int I = 0; I < 10; i++) { System.out.println(“I = “ +I); } No difference in for loop but there is a difference when using in an expression i++ - Use the current value of I in the expression then increment by 1 for(int I = 0; I < 10; ++i) { System.out.println(“I = “ +I); } No difference in for loop ++I – increment by 1, the nuse the new value in the expression Comparing I++ and ++I
Sorting an Array • One of the most important computing applications • Sorting data is computer intensive -- much research has gone into finding good algorithms • Choice of algorithm affects • Run time • Memory usage
Sorting Algorithms • Selection Sort • Insertion Sort • Merge Sort
Big-O • Describes and algorithms efficiency in terms of the work required to solve a problem
Selection Sort Select sort runs in O(n2) time The outer loops runs n-1 times The inner loop runs n -1 times, then n- 2 times, then n-3 times…. 1 Inner loops runs a total of n(n-1)/2 times In Big-O notation, the smaller term drops out so it is O(n2)
Insertion Sort • Runs in O(n2) • Outer loop runs n-1 times • Worst case, while loop runs n-1 times
Next Time • Merge sort • Bubble sort
Conway’s Game of Life • Partner Problem • Solution is on the web • You will need to explain HOW you and your partner created a solution to the Game of Life in detail. • There will be a Game of Life “Quiz” on the due date where you will answer these questions