Sessions 26 and 27 - Data Structures & Algorithm

Sharif university of technology advanced programming

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: rithms One of the most important computing applications Virtually every organization must sort some data, and often, massive amounts of it An intriguing, computer-intensive problem that has attracted attracted intense research efforts. The choice of algorithm affects the runtime and memory use of the program Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 94 Efficiency of Selection Sort Easy-to-implement, but inefficient Idea: Find the maximum (minimum) of the list and move upward Example 4 3 8 1 6 Sharif University of Technology 1 3 8 4 6 1 3 8 4 6 1 3 4 8 6 1 3 4 6 8 Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 95 Efficiency of Selection Sort The selection sort algorithm iterates n - 1 times Each time swapping the smallest remaining element into its sorted position Locating the smallest remaining element requires n - 1 comparisons during the first iteration, n - 2 during the second iteration, then n - 3, ..., 3, 2, 1 A total of n(n 1)/2 or (n2 n)/2 comparisons In Big O notation, smaller terms drop out and constants are ignored, leaving a final Big O of O(n2) Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 96 Efficiency of Insertion Sort Insertion sort iterates n - 1 times For each iteration, determining where to insert the element can require comparing the element to each of the preceding elements in the vector In the worst case, this will require n - 1 comparisons for each iteration to find insertion point For each O(n) iteration of the outer loop, there will be O(n) iterations of the inner loop, resulting in a Big O of O(n* n) or O(n2). Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 97 Efficiency of Insertion Sort Another simple, but inefficient, sorting algorithm Read values Insert them in the correct position Final list is sorted Example: 8, 4, 5, 3, 7, 2 8 4 3 5 7 2 4 8 3 5 7 2 Sharif University of Technology 3 4 8 5 7 2 3 4 5 8 7 2 3 4 5 7 8 2 Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 2 3 4 5 7 8 98 Merge Sort An efficient sorting algorithm Sorts a vector by splitting it into two equal-sized subvectors The implementation of merge sort in this example is recursive The base case is a vector with one element Follow a divide-and-conquer approach Break the problem into several subproblems Similar to original problem Smaller in size Solve the subproblems recursively Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 99 Merge Sort Divide-and-Conquer approach Divide subproblems Conquer solve them recursively Combine combine the solution Merge Sort Divide: n-element sequence to be sorted into two subsequences of n/2 elements Conquer: Sort two subsequences recursively Combine: Merge two sorted subsequences Sharif University of Technology Advanced Programming: Sessions 2...
View Full Document

This note was uploaded on 09/01/2013 for the course CE CE230 taught by Professor Arash during the Fall '13 term at Sharif University of Technology.

Ask a homework question - tutors are online