100 likes | 223 Vues
Sorting algorithms are crucial in computer science, impacting run time and memory usage. This article examines key sorting algorithms—Selection Sort, Insertion Sort, and Merge Sort—highlighting their efficiency in Big-O notation. Selection Sort operates in O(n²), showcasing an outer loop and inner loop mechanism for sorting. Insertion Sort also runs in O(n²), particularly in its worst-case scenario. Explore how these algorithms function and the tasks involved in sorting data efficiently. Understanding these concepts is vital for computer-intensive applications and optimal performance.
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