Escolar Documentos
Profissional Documentos
Cultura Documentos
Objectives:
sort an array into ascending order by using the following methods: selection sort, insertion sort, shell sort, merge sort, quick sort and heap sort, sort a chain of nodes into ascending order by using an insertion sort, and determine the efficiency of a sort and comparing the algorithms of sorting.
Introduction
Sorting rearranging data in an array or list so that it is in ascending or descending order. Sorting can be done to any collection of items that can be compared with one another. Eg: arrange a row of books on bookshelf by title. The efficiency of a sorting algorithm is significant, particularly when large amounts of data are involved.
INSERTION SORT
Insertion Sort
Remove second book Slide first book to right Insert removed book into first slot Remove that book Slide 2nd book to right Insert removed book into 2nd slot Etc.
Insertion Sort
Insertion Sort
Insertion Sort
Sort an array partitions that is, divides the array into two parts.
One part is sorted and initially contains just first element in the array The second part contains the remaining elements.
The algorithm removes the first element from the unsorted part and inserts it into its proper sorted position within the sorted part. An insertion sort is more efficient when an array is sorted.
public class Sort { public static void main(String [] args) { int [] tsnom = {3, 1, 2, 7, 5}; System.out.println(Before Insertion Sort"); print(tsnom); insertionSort(tsnom); System.out.println(After Insertion Sort"); print(tsnom); } public static void insertionSort (int[] ts) { for (int x=1; x < ts.length; x++) { int temp=ts[x]; int y=x; for (; y>0 && temp < ts[y-1];y--) ts[y]=ts[y-1]; ts[y]=temp; } } public static void print(int[] ts) { for(int x=0; x< ts.length; x++) System.out.print(ts[x]); System.out.println(); } }
Insertion Sort
3 1 2 7 5 [0] [1] [2] [3] [4] int temp=ts[x]; int y=x; for (; y>0 && temp < ts[y-1];y--) ts[y]=ts[y-1]; ts[y]=temp
x=1 y=1 y=0
x=3 y=3
X=3
temp=7 7<3
1 3 3 7 5 [0] [1] [2] [3] [4] 1 2 3 7 5 [0] [1] [2] [3] [4]
X=4
Insertion Sort
int temp=ts[x]; int y=x; for (; y>0 && temp < ts[y-1];y--) ts[y]=ts[y-1]; ts[y]=temp 3 1 2 7 5 [0] [1] [2] [3] [4]
X=1 temp=1 1<3
During the traversal of a chain to locate the insertion point, save a reference to the node before the current one.
Breaking a chain of nodes into two pieces as the first step in an insertion sort: (a) the original chain; (b) the two pieces
Best time efficiency is O(n) Worst time efficiency is O(n2) Average time efficiency is O(n2) If array is closer to sorted order
Less work the insertion sort does More efficient the sort is
SELECTION SORT
Selection Sort
Rearranges the object by interchanging value. The sort locates the smallest/largest value in the array. Task: rearrange books on shelf by height
Shortest book on the left Look at books, select shortest book Swap with first book Look at remaining books, select shortest Swap with second book Repeat
Approach:
Selection Sort
Before and after exchanging shortest book and the first book.
Selection Sort
Before Sort
L1
L2
L3
L4
Sorted List
12
12
12
12
12
12
19 33
19 33
19 22
19 22
19 22
19 22
26 22
26 22
26 33
26 33
26 33
26 33
Max=33
Max=26
Max=22
Max=19
Selection Sort
public void selectionSort(Object array[]) { int minIndex; Object temp; for(int x = 0; x < array.length; x++) { minIndex = 0; //initialization the index of smallest value for(int y = x + 1; y < array.length; y++){ //find the index of the smallest value in the list if(array[y] < array[minIndex]) minIndex = y; } //move the smallest value into correct position (swap) temp = array[x]; array[x] = array[minIndex]; array[minIndex] = temp; } }//end selectionSort
For each of n 1 calls, inner loop executes n 2 times (n 1) + (n 2) + + 1 = n(n 1)/2 = O(n2) Also O(n2)
Best time efficiency is O(n2) Worst time efficiency is O(n2) Average time efficiency is O(n2) Sufficient when to sort a small array.
SHELL SORT
Shell Sort
Done by sorting subarrays of equally spaced indices Instead of moving to an adjacent location an element moves several locations away
Results in an almost sorted array This array sorted efficiently with ordinary insertion sort
Shell Sort
List (Before Sort) 81 94 11 96 12 35 17 95 28 58 41 75 15
horizontal
12 58
41
75
15
vertical
12 58
horizontal
15 96 58 81 94 95
Shell Sort
35 28 75 58 95
17 12 15 81
11 41 96 94
12
15 17 81
11
41 94 96
Shell Sort
Best time efficiency is O(n) Worst time efficiency is O(n1.5) Average time efficiency is O(n1.5)
MERGE SORT
Merge Sort
Sort the two halves Merge them into one sorted array This is often part of a recursive algorithm However recursion is not a requirement
Merge Sort
Algorithm mergeSort(a, first, last) // Sorts the array elements a[first] through a[last] recursively.
Merge Sort
The effect of the recursive calls and the merges during a merge sort.
Merge Sort
Merge sort is O(n log n) in all cases It's need for a temporary array is a disadvantage The class Arrays has sort routines that uses the merge sort for arrays of objects
public static void sort(Object[] a); public static void sort(Object[] a, int first, int last);
public class MergeSort { public static void main(String [] args) { int [] tsnom = {34,8,64,51,32,21,30}; System.out.println(Before Merge Sort"); print(tsnom); mergeSorting(tsnom); System.out.println(After Merge Sort"); print(tsnom); } private static void mergeSorting(int []a, int[]tsSem, int left, int right) { if (left < right) { int mid = (left + right)/2; mergeSorting(a,tsSem,left,mid); // left side mergeSorting(a,tsSem,mid + 1, right); // right side merge(a,tsSem,left,mid + 1, right); } } public static void mergeSorting(int []a) { Java Code for Merge int [] tsSem=new int[a.length]; Sort continued mergeSorting(a,tsSem,0,a.length-1); next slide }
private static void merge(int [] a, int [] tsSem, int posleft, int posright, int lastright) { int lastleft=posright - 1; int possem=posleft; int bilelemen=lastright - posleft + 1; while(posleft <= lastleft && posright <= lastright) { if (a[posleft]<=a[posright]) tsSem[possem++]=a[posleft++]; else tsSem[possem++]=a[posright++]; } while(posleft <=lastleft) tsSem[possem++]=a[posleft++]; while(posright<=lastright) tsSem[possem++]=a[posright++]; for(int i=0;i<bilelemen;i++,lastright--) a[lastright]=tsSem[lastright]; }//merge public static void print(int[]a) { for (int i=0;i<a.length;i++) System.out.print("\t"+ a[i]); }//print } // class MergeSort
Merge Sort
Merge Sort has two level:a) Divide the list into halves
2 5 [0] [1]
1 9 [2] [3]
3 8 [4] [5]
7 [6]
L=6 R=6
2 [0]
L=0 R=0
5 [1]
L=1 R=1
1 [2]
L=2 R=2
9 [3]
L=3 R=3
3 [4]
L=4 R=4
8 [5]
L=5 R=5
First example
2 5 [0] [1]
1 9 [2] [3]
3 8 [4] [5]
7 [6]
2 [0]
5 [1]
1 [2]
9 [3]
3 [4]
8 [5]
First example
Merge Sort
34 8 [0] [1]
64 51 [2] [3]
32 21 [4] [5]
30 [6]
L=6 R=6
L=0 R=0
34 [0]
8 [1]
L=1 R=1
64 [2]
L=2 R=2
51 [3]
L=3 R=3
32 [4]
L=4 R=4
21 [5]
L=5 R=5
Second example
Merge Sort
8 34 [0] [1]
51 64 [2] [3]
21 32 [4] [5]
30 [6]
L=6 R=6
L=0 R=0
34 [0]
8 [1]
L=1 R=1
64 [2]
L=2 R=2
51 [3]
L=3 R=3
32 [4]
L=4 R=4
21 [5]
L=5 R=5
Second example
Best time efficiency is O(n log n) Worst time efficiency is O(n log n) Average time efficiency is O(n log n)
QUICK SORT
Quick Sort
Not necessarily halves of the array An element of the array is selected as the pivot
Elements in positions before pivot are less than the pivot Elements after the pivot are greater than the pivot
Quick Sort
Quick Sort
Algorithm quickSort(a, first, last) // Sorts the array elements a[first] through a[last] recursively. if (first < last) { Choose a pivot Partition the array about the pivot pivotIndex = index of pivot quickSort(a, first, pivotIndex-1) // sort Smaller quickSort(a, pivotIndex+1, last) // sort Larger
Quick Sort
Quick sort is O(n log n) in the average case and best case O(n2) in the worst case Worst case can be avoided by careful choice of the pivot
Quick Sort
Quick sort rearranges the elements in an array during partitioning process After each step in the process
One element (the pivot) is placed in its correct sorted position Remain in their respective subarrays
The class Arrays in the Java Class Library uses quick sort for arrays of primitive types
Quick Sort
Quick Sort has two level:a) Divide the list into halves (the first number is a pivot)
Quick Sort
b) Merge/combine the list from bottom
public class QuickSort { public static void main(String [] args) { int [] tsnom = {33,17,6,21,56,29}; System.out.println(Before Quick Sort"); print(tsnom); quickSorting(tsnom,0,tsnom.length-1); System.out.println(After Quick Sort"); print(tsnom); } private static void quickSorting(int []a, int first, int last) { int indeksPivot; if (first < last) { indeksPivot=choosePivot(a,first,last); quickSorting(a,first,indeksPivot-1); quickSorting(a,indeksPivot+1,last); }//if }//quickSorting
//choosePivot method for chosing the first number and sort private static int choosePivot(int []a,int first, int last) { int p=first; int pivot=a[first]; //for sort, left is smaller then pivot and right for greater then pivot for (int i=first + 1; i <=last; i++) { if (a[i] < pivot) { a[p]=a[i]; a[i]=a[p+1]; a[p+1]=pivot; p++; } } return p; }//choosePivot public static void print(int[]a) { for (int i=0;i<a.length;i++) System.out.print("\t"+ a[i]); System.out.println("\n"); }//print }
Quick Sort
List of Number Pivot = 5 and the value of pivot is smaller than other numbers. 5 6 7 8 9
pivot
unknown
5
pivot
unknown
5
pivot
unknown
5
pivot
9
unknown
5
pivot After first partition
Quick Sort
5 6 7 8 9 [0] [1] [2] [3] [4] <5 6 <6 7 <7 8 <8 >5 7 8 9 >6 8 9
>7 9 >8 9
Best time efficiency is O(n log n) Worst time efficiency is O(n2) Average time efficiency is O(n log n)
HEAP SORT
The ADT heap The heap of the operating system from which memory is allocated when new executes
Figure-1: (a) A complete binary tree with its nodes numbered in level order; (b) its representation as an array.
Parent of a node at i is found at i/2 (unless i is 1) Children of node at i found at indices 2i and 2i + 1
Adding an Entry
Adding an Entry
Begin at next available position for a leaf Follow path from this leaf toward root until find correct position for new entry As this is done
Move entries from parent to child Makes room for new entry
Adding an Entry
Adding an Entry
Adding an Entry
Adding an Entry
Figure-5: The steps to remove the entry in the root of the maxheap of Figure-3(d)
Figure-6: The steps that transform a semiheap into a heap without swaps.
Creating a Heap
Figure-7: The steps in adding 20, 40, 30, 10, 90, and 70 to a heap.
Creating a Heap
Heapsort
Possible to use a heap to sort an array Place array items into a maxheap Then remove them
Items will be in descending order Place them back into the original array and they will be in order
Heapsort
Heapsort
Heapsort
Heapsort
Heapsort
Implementation of heapsort
public static void heapSort(Comparable[] array, int n) { // create first heap for (int index = n/2; index >= 0; index--) reheap(array, index, n-1); swap(array, 0, n-1); Efficiency is O(n log n). for (int last = n-2; last > 0; last--) { reheap(array, 0, last); However, quicksort is swap(array, 0, last); usually a better choice. } // end for } // end heapSor private static void reheap(Comparable[] heap, int first, int last) { < Statements from Segment 27.11, replacing rootIndex with first and lastIndex with last. > } // end reheap
Heap Sort
Creating a heap
1 2 4
5
3 6
7 9
2
5 3
1
7
8 reheap(3)
1 2 4
5
3 6
7 9
2
5 3
8
7
7 reheap(2)
Heap Sort
Creating a heap
1 2 4
5
3 6
7 2
9
5 3
8
7
reheap(1)
1 2 4
9
3 6
5
5 3
8
7
Heap Sort
Creating a heap
1 2 4
9
3 6
7 2
5
5 3
8
7
1 remove
1 2 4
7
3 6
9 2
5
5 3
Heap Sort
Creating a heap
1 2
7
3 6
9
4
5
5 3
1 reheap(1) 8
3 6
1 2 4
9 2
5
5 3
1 remove
Heap Sort
Creating a heap
1 2 4
1
3
9
2
5
5 3
reheap(1)
Heap Sort
Creating a heap
1 2
7
3
9
4
5
5 3
remove
1
2 4
3
3
9 2
reheap(1)
Heap Sort
Creating a heap
1 2 4
5
3
9 2
remove
1
2
2
3
reheap(1)
Heap Sort
Creating a heap
1 2
3
3
2 remove
reheap(1)
Heap Sort
Creating a heap
1 2
remove
remove
Best time efficiency is O(n log n) Worst time efficiency is O(n log n) Average time efficiency is O(n log n)
Insertion sort Selection sort Shell sort Merge sort Quick sort Heap sort