Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Sorting is a fundamental problem in computer science.

Similar presentations


Presentation on theme: "Sorting Sorting is a fundamental problem in computer science."— Presentation transcript:

1 Sorting Sorting is a fundamental problem in computer science.
comparison-based sorting: the only operations allowed on the input data are: <, > operations assignment input data must be comparable. 15 2 8 1 17 10 12 5 3 4 6 7 Nov 30, 2001 CSE 373, Autumn 2001

2 Insertion Sort Simple sorting algorithm. N-1 passes over the array
At the end of pass p, the elements that occupied A[0]…A[p] originally are still in those spots and in sorted order. 2 8 15 1 17 10 12 5 3 4 6 7 after pass 2 1 2 8 15 17 10 12 5 3 4 6 7 after pass 3 Nov 30, 2001 CSE 373, Autumn 2001

3 Analysis Insertion sort worst case time:
Insertion sort best case time: array is in sorted order. O(N). comparisons assignments insertion worst: N2/2 best: N selection N2/2 N Nov 30, 2001 CSE 373, Autumn 2001

4 Average case analysis Given an array A of elements, an inversion is an ordered pair (i, j) such that i < j, but A[i] > A[j]. Assume no duplicate elements. Theorem: The average number of inversions in an array of N distinct elements is N(N-1)/4. Corollary: Any algorithm that sorts by exchanging adjacent elements requires (N2) time on average. Nov 30, 2001 CSE 373, Autumn 2001


Download ppt "Sorting Sorting is a fundamental problem in computer science."

Similar presentations


Ads by Google