Title: INFSCI 0015 Data Structures Lecture 13: Sorting
1INFSCI 0015 - Data StructuresLecture 13 Sorting
- Peter Brusilovsky
- http//www2.sis.pitt.edu/peterb/0015-011/
2Outline
- Solution for 5.1
- About Sorting
- Insertion Sort
- Selection Sort
- Bubble Sort
- One-Array Solution for 5.1
3Problem 5.1 Partition Array
- include ltstdio.hgt
- define N 10 / dimension of the array /
- void readarray(int ar, int n)
- void partition_array(int ar, int n)
- void printarray(int ar, int n)
- int odd(int a)
- int even(int a)
- main()
- int testarrayN / elements from ar0 to
arN-1 / - readarray(testarray, N)
- printf("Before partitioning, numbers are ")
- printarray(testarray, N)
- partition_array(testarray, N)
- printf("After partitioning, numbers are ")
- printarray(testarray, N)
4Problem 5.1 Partition Array
1
2
3
4
5
6
7
1
3
5
7
2
4
6
5Problem 5.1 Partition Array
1
3
5
7
2
4
6
1
3
5
7
2
4
6
6Problem 5.1 Partition Array
- void readarray(int ar, int n_of_elements)
- int i
- for (i 0 i lt n_of_elements i)
- printf("dgt ", i)
- scanf("d", ari)
-
- return
-
- void printarray(int ar, int n_of_elements)
- int i
- for (i 0 i lt n_of_elements i)
- printf ("d ", ari)
- printf("\n")
- return
-
- int odd(int a)
- return(a 2)
7Problem 5.1 Partition Array
- void partition_array(int ar, int n)
- int i, j, k
- int oddarN, evenarN
- j k 0
- for (i 0 i lt n i)
- if (odd(ari))
- oddarj ari j
- else
- evenark ari k
-
- / copying odd array /
- for(i 0 i lt j i)
- ari oddari
- / copying even array /
- for(i 0 i lt k i)
- arji evenari
- return
8Sort Classification
9Sort Stability
- Does it keep the same order for data with equal
keys?
10Straight Insertion Sort
- Array is divided sorted / unsorted
- At each step an element from unsorted part is
inserted in its place in sorted part
11(No Transcript)
12GF Driver for Sort Exploration
- int main ( void )
-
- int i
- int ary MAX_ARY_SIZE 89, 72, 3, 15, 21,
57, 61, 44, 19, 98, 5, 77, 39, 59, 61 - printf( "Unsorted array " )
- for ( i 0 i lt MAX_ARY_SIZE i )
- printf( "3d", ary i )
-
- insertionSort ( ary, MAX_ARY_SIZE - 1) / call
of some sort / - printf( "\nSorted array " )
- for ( i 0 i lt MAX_ARY_SIZE i )
- printf( "3d", ary i )
- printf( "\n" )
- return 0
-
13Insertion Sort Code
- void insertionSort (int list , int last )
- int current / index for outer loop /
- int hold / here well store the element to be
inserted / - int walker / index for inner moving loop /
- for ( current 1 current lt last current )
- hold listcurrent / element to be
inserted / - / moving array elements up to free space /
- for (walker current - 1
- walker gt 0 hold lt listwalker
walker--) - listwalker 1 listwalker
- / inserting the element into its right
place / - list walker 1 hold
- / for current /
- return
-
14Complexity for insertion sorts
- Complexity for straight insertion
- 12n-1
- n2/2 ? O(n2 )
- Complexity for Shell sort
- O(n1.25 )
15Selection Sort (MinSort)
- Array is divided sorted / unsorted
- At each step we find min element in the unsorted
part and swap it with the first
16Figure 11-9
17Selection Sort (Min) Code
- void selectionSort (int list , int last )
- int current
- int smallest
- int holdData
- int walker
- for ( current 0 current lt last current )
- / Finding smallest element in the unsorted
part / - smallest current
- for (walker current 1 walker lt last
walker) - if ( list walker lt list smallest )
- smallest walker
- / Smallest selected exchange with current /
- holdData list current
- listcurrent list smallest
- listsmallest holdData
- / for current /
- return
-
18Complexity for selection sorts
- Complexity for straight selection
- 12n-1
- n2/2 ? O(n2 )
- Complexity for heap sort
- O(nlog2n)
19Bubble Sort
- Array is divided sorted / unsorted
- At each step the smallest element is bubbled up
to the sorted sublist
20(No Transcript)
21Bubble Sort Code
- void bubbleSort (int list , int last)
- int current int sorted
- int walker int temp
- / Each iteration is one sort pass /
- for(current 0, sorted 0 current lt last
!sorted current) - for(walker last, sorted 1 walker gt current
walker--) - if ( list walker lt list walker - 1 )
- / Any exchange means list is not sorted /
- sorted 0
- temp listwalker
- listwalker listwalker - 1
- listwalker - 1 temp
- / if /
- return
22Complexity for exchange sorts
- Complexity for bubble sort
- O(n2 )
- Complexity for quick sort
- O(nlog2n)
23External Sorts Merging Files
24Using exchange approach for 5.1
- / Partition Array /
- void partition_array(int ar, int n)
- int i, sw, swaps 1
- while(swaps gt 0)
- swaps 0 / initializing number of swaps /
- for (i 1 i lt n i)
- if (odd(ari) even(ari-1))
- sw ari-1
- ari-1 ari
- ari sw
- swaps
-
-
- return