sorting - Internal Sorting Algorithms A sorting algorithm...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Internal Sorting Algorithms A sorting algorithm can be classified as being internal if the records that it is sorting are in main memory, or external if some of the records that it is sorting are in secondary storage. In this course, we shall only discuss internal sorting algorithms. To simplify discussion, sorting of an integer array is used in our examples. Efficiency of a sorting method is usually measured by the number of comparisons and data movements required. Insertion sort Successively insert a new element into a sorted sublist. Example: input array: [25 57 48 37 92 60] sorted sublist: [25] ; initial condition sorted sublist: [25 57] ; insert 57 sorted sublist: [25 48 57] ; insert 48 sorted sublist: [25 37 48 57] ; insert 37 sorted sublist: [25 37 48 57 92] ; insert 92 sorted sublist: [25 37 48 57 60 92] ; insert 60 void insertionSort(int x[], int N) { for (int i = 1; i < N; i++) { int t = x[i]; int j; for (j = i-1; j >= 0 && x[j] > t; j--) x[j+1] = x[j]; x[j+1] = t; } }
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Complexity of insertion sort Let c be the number of comparisons required to insert an element into a sorted sublist of size k . Best case: c = 1 Average case: c = k /2 Worst case: c = k The total number of comparisons = 1 1 N k c Overall time complexity Best case c = 1 O ( N ) Average case c = k /2 O ( N 2 ) Worst case c = k O ( N 2 ) Number of data movement = number of comparisons Because of the simplicity of insertion sort, it is the fastest sorting method when the number of elements N is small, e.g. N < 10. Generic sorting function for any data type template<class Type> void insertionSort(Type *x, unsigned N, int (*compare)(const Type&, const Type&)) { for (int i = 1; i < N; i++) { Type t = x[i]; int j; for (j = i-1; j >= 0 && (*compare)(x[j], t) > 0; j--) x[j+1] = x[j]; x[j+1] = t; } }
Image of page 2
3 Quicksort Consider an array x [0 ..N 1] 1. Choose a pivot element a from a specific position, say a=x [0] 2. partition x [] using a, i.e. a is placed into position j ( i.e. x [ j ] = a ) such that x [ i ] a for i = 1, 2, …, ( j 1), and x [ k ] > a where k = j +1, …, N-1 3. The two subarrays x [0 .. ( j 1)] and x [( j+ 1) ..N 1] are sorted recursively using the same method. Partitioning operation: Step 1: scan the array from left to right to look for an element x[ i ] > x[0] Step 2: scan the array from right to left to look for an element x[ j ] x[0] Step 3: if ( i < j ) swap x[ i ] with x[ j ] and go to step 1; otherwise swap x[0] with x[ j ] (partitioning finished). Example: Partitioning process   50 25 57 48 37 92 33 64 ;swap x[ i ] with x[ j ] i j 50 25 33 48 37 92 57 64 ;swap x[0] with x[ j ] j i 37 25 33 48 50 92 57 64 ;partitioning finished [ 50 ] [ > 50 ] ;sort the left and right sublists recursively left sublist right sublist
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 void swap(int x[], int i, int j) { int t = x[i]; x[i] = x[j];
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern