csc 211 data structures lecture 14
DESCRIPTION
CSC 211 Data Structures Lecture 14. Dr. Iftikhar Azim Niaz [email protected]. 1. Last Lecture Summary. Binary Search Concept Algorithm Implementation of Binary search Complexity of Binary Search Comparison of Linear and Binary Search Searching Unordered Linked List - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/2.jpg)
2
Last Lecture Summary Binary Search
Concept Algorithm
Implementation of Binary search Complexity of Binary Search Comparison of Linear and Binary Search Searching Unordered Linked List Searching Ordered Linked List
2
![Page 3: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/3.jpg)
3
Objectives Overview Sorting
Concept Reasons for Sorting Basic Terminology Sorting Classification Stability of Key Bubble Sort
Concept Algorithm Code and Implementation
![Page 4: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/4.jpg)
4
Sorting A fundamental operation in computer science Task of rearranging data in an order such as
Ascending Descending or Lexicographic
Data may be of any type like numeric, alphabetical or alphanumeric
It also refers to rearranging a set of records based on their key values when the records are stored in a file
Sorting task arises more frequently in the world of data manipulation
4
![Page 5: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/5.jpg)
5
Sorting Sorting and searching frequently apply to a file of
records Each record in a file F can contain many fields
but there may be one particular field whose values uniquely determine the records in the file called primary key
The key or key values are k1, k2, …… Sorting the file F usually refers to sorting F with
respect to a particular primary key Searching in F refers to searching for a record
with a given key value5
![Page 6: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/6.jpg)
6
Sorting Let A be a list of n elements in memory
A1, A2, ……., An
Sorting refers to the operations of rearranging the contents of A so that they are increasing in order numerically or lexicographically so that A1 A2 A3 ………….. An
Since A has n elements, there are n! ways that contents can appear in A
These ways correspond precisely to the n! permutations of 1, 2, …., n
Accordingly each sorting algorithms must take care of these n! possibilities
6
![Page 7: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/7.jpg)
7
Sorting Efficient sorting is important for optimizing the
use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly;
Sorting is also often useful for canonicalizing data and for producing human-readable output.
More formally, the output must satisfy two conditions: The output is in non-decreasing order (each element
is no smaller than the previous element according to the desired total order);
The output is a permutation (reordering) of the input.
![Page 8: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/8.jpg)
8
Reasons for Sorting From the programming point of view, the
sorting task is important for the following reasons How to rearrange a given set of data? Which data structures are more suitable to store
data prior to their sorting? How fast can the sorting be achieved? How can sorting be done in a memory constrained
situation? How to sort various type of data?
![Page 9: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/9.jpg)
9
Basic Terminology - I Internal sort
When a set of data to be sorted is small enough such that the entire sorting can be performed in a computer’s internal storage (primary memory)
External sort Sorting a large set of data which is stored in low
speed computer’s external memory such as hard disk, magnetic tape, etc.
Ascending order An arrangement of data if it satisfies “less than or
equal to <=“ relation between two consecutive data [1, 2, 3, 4, 5, 6, 7, 8, 9]
![Page 10: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/10.jpg)
10
Basic Terminology - II Descending order
An arrangement of data if it satisfies “greater than or equal to >=“ relation between two consecutive data
e.g. [ 9, 8, 7, 6, 5, 4, 3, 2, 1] Lexicographic order
If the data are in the form of character or string of characters and are arranged in the same order as in dictionary
e.g. [ada, bat, cat, mat, max, may, min] Collating sequence
Ordering for a set of characters that determines whether a character is in higher, lower or same order compared to another
e.g. alphanumeric characters are compared according to their ASCII code
e.g. [AmaZon, amaZon, amazon, amazon1, amazon2]
![Page 11: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/11.jpg)
11
Basic Terminology - III Random order
If a data in a list do not follow any ordering mentioned above, then it is arranged in random order
e.g. [8, 6, 5, 9, 3, 1, 4, 7, 2] [may, bat, ada, cat, mat, max, min]
Swap Swap between two data storages implies the interchange of
their contents. e.g. Before swap A[1] = 11, A[5] = 99 After swap A[1] = 99, A[5] = 11
Item Is a data or element in the list to be sorted. May be an integer, string of characters, a record etc. Also alternatively termed key, data, element etc.
![Page 12: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/12.jpg)
12
Basic Terminology - IV Stable Sort
A list of data may contain two or more equal data. If a sorting method maintains the same relative position of their occurrences in the sorted list then it is stable sort
e.g. [ 2, 5, 6, 4, 3, 2, 5, 1, 5 ]
[ 1, 2, 2, 3, 4, 5, 5, 5, 6 ] In Place Sort
Suppose a set of data to be sorted is stored in an array A If a sorting method takes place within the array A only,
i.e. without using any other extra storage space It is a memory efficient sorting method
![Page 13: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/13.jpg)
13
Sorting Methods
Bubble Sort Heap Sort
Selection Sort Merge Sort
Insertion Sort Quick Sort
![Page 14: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/14.jpg)
14
Sorting Classification - I Sorting algorithms are often classified by:
Computational complexity (worst, average and best behavior) of element comparisons in terms of the size of the list (n).
For typical sorting algorithms good behavior is O(n log n) and bad behavior is O(n2). Ideal behavior for a sort is O(n), but this is not possible
in the average case. Comparison-based sorting algorithms, which evaluate
the elements of the list via an abstract key comparison operation, need at least O(n log n) comparisons for most inputs.
![Page 15: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/15.jpg)
15
Sorting Classification - II Computational complexity of swaps (for "in place"
algorithms). Memory usage (and use of other computer resources).
In particular, some sorting algorithms are "in place” Strictly, an in place sort needs only O(1) memory
beyond the items being sorted; sometimes O(log(n)) additional memory is considered
"in place” Recursion. Some algorithms are either recursive or non-
recursive, while others may be both (e.g., merge sort). Stability: stable sorting algorithms maintain the relative
order of records with equal keys (i.e., values)
![Page 16: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/16.jpg)
16
Sorting Classification - III Whether or not they are a comparison sort.
A comparison sort examines the data only by comparing two elements with a comparison operator.
General method: insertion, exchange, selection, merging, etc. Exchange sorts include bubble sort and quicksort. Selection sorts include shaker sort and heapsort.
Adaptability: Whether or not the presortedness of the input affects the running time. Algorithms that take this into account are known to be adaptive.
![Page 17: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/17.jpg)
17
Stability Stable sorting algorithms maintain the relative
order of records with equal keys. A key is that portion of record which is the basis for the
sort it may or may not include all of the record
If all keys are different then this distinction is not necessary.
But if there are equal keys, then a sorting algorithm is stable if whenever there are two records (let's say R and S) with the same key, and R appears before S in the original list, then R will always appear before S in the sorted list.
![Page 18: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/18.jpg)
18
Stability -II When equal elements are indistinguishable, such
as with integers, or more generally, any data where the entire element is the key, stability is not an issue.
However, assume that the following pairs of numbers are to be sorted by their first component:
(4, 2) (3, 7) (3, 1) (5, 6) Two different results are possible,
one which maintains the relative order of records with equal keys, and one which does not:
(3, 7) (3, 1) (4, 2) (5, 6) (order maintained) (3, 1) (3, 7) (4, 2) (5, 6) (order changed)
![Page 19: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/19.jpg)
19
Stability - III Unstable sorting algorithms may change the
relative order of records with equal keys, but stable sorting algorithms never do so.
Unstable sorting algorithms can be specially implemented to be stable. One way of doing this is to artificially extend the key
comparison, so that comparisons between two objects with otherwise equal keys are decided using the order of the entries in the original data order as a tie-breaker.
Remembering this order, however, often involves an additional computational cost.
![Page 20: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/20.jpg)
20
Stability - IV Sorting based on a primary, secondary, tertiary, etc. sort key
can be done by any sorting method, taking all sort keys into account in comparisons in other words, using a single composite sort key
If a sorting method is stable, it is also possible to sort multiple times, each time with one sort key. In that case the keys need to be applied in order of increasing priority.
Example: sorting pairs of numbers as above by second, then first component:
(4, 2) (3, 7) (3, 1) (5, 6) (original) (3, 1) (4, 2) (5, 6) (3, 7)
after sorting by second component (3, 1) (3, 7) (4, 2) (5, 6)
after sorting by first component
![Page 21: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/21.jpg)
21
Stability - V On the other hand: (4, 2) (3, 7) (3, 1) (5, 6) (original) (3, 7) (3, 1) (4, 2) (5, 6)
after sorting by first component (3, 1) (4, 2) (5, 6) (3, 7)
after sorting by second component order by first component is disrupted
![Page 22: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/22.jpg)
22
Bubble Sort Sometimes incorrectly referred to as sinking sort,
is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
The algorithm gets its name from the way smaller elements "bubble" to the top of the list.
Because it only uses comparisons to operate on elements, it is a comparison sort.
![Page 23: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/23.jpg)
23
Bubble Sort Bubble sort is a simple sorting algorithm The algorithm starts at the beginning of the data
set. It compares the first two elements, and if the first is
greater than the second, it swaps them. It continues doing this for each pair of adjacent elements
to the end of the data set. It then starts again with the first two elements, repeating
until no swaps have occurred on the last pass. Note that the largest end gets sorted first, with
smaller elements taking longer to move to their correct positions.
![Page 24: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/24.jpg)
24
Bubble Sort Algorithm Suppose the list of numbers A[1], A[2], …. A[N] is in
memory. The bubble sort algorithm works as follows: Step 1: Compare A[1] and A[2] and arrange them in
the desired order, so that A[1] < A[2]. Then compare A[2] and A[3] and arrange them so that
A[2] < A[3]]. Then compare A[3] and A[4] and arrange them so that A[3] < A[4]. Continue until we compare A[N – 1] with A[N] and arrange them so that A[N – 1] < A[N].
Observe that Step 1 involves n – 1 comparisons. During Step 1, the largest element is “bubbled up” to the nth position or “sinks” to the nth position.
When Step 1 is completed, A[N] will contain the largest element
![Page 25: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/25.jpg)
25
Bubble Sort Algorithm Step 2: Repeat Step 1 with one less
comparison . i.e. now we stop after we compare and possible rearrange A[N - 2] and A[N - 1].
Step 2 involves N – 2 comparisons and when Step 2 is completed, A[N - 1] will contain the second largest element.
Step 3: Repeat Step 1 with two fewer comparisons . i.e. we stop after we compare and possible rearrange A[N - 3] and A[N - 2]
Step 3 involves N – 3 comparisons and when Step 2 is completed, A[N - 1] will contain the third largest element
![Page 26: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/26.jpg)
26
Bubble Sort Algorithm …………………………………………………. …………………………………………………. Step N - 1:Compare A[1] with A[2] and arrange
them so that A[1] < A[2]. After n -1 steps the list will be sorted in the
ascending order The process of sequentially traversing through
all or part of a list is called a “pass” so each of the above step is called a pass
Accordingly, the bubble sort algorithm requires n – 1 passes where n is the number of input items
![Page 27: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/27.jpg)
27
Bubble Sort
![Page 28: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/28.jpg)
28
Bubble Sort Step-by-Step Example Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort
In each step, elements written in bold are being compared
Three passes will be required First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
![Page 29: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/29.jpg)
29
Bubble Sort Step-by-Step Example Second Pass:( 1 4 2 5 8 ) ( 1 4 2 5 8 )( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )Now, the array is already sorted, but our algorithm does not know if it is completed
The algorithm needs one whole pass without any swap to know it is sorted
Third Pass:
( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )
![Page 30: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/30.jpg)
30
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
![Page 31: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/31.jpg)
31
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Swap?
Bubble Sort Algorithm
![Page 32: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/32.jpg)
32
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Yes!
Bubble Sort Algorithm
![Page 33: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/33.jpg)
33
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Swap?
Bubble Sort Algorithm
![Page 34: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/34.jpg)
34
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
No.
Bubble Sort Algorithm
![Page 35: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/35.jpg)
35
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Swap?
Bubble Sort Algorithm
![Page 36: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/36.jpg)
36
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
No.
Bubble Sort Algorithm
![Page 37: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/37.jpg)
37
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Swap?
Bubble Sort Algorithm
![Page 38: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/38.jpg)
38
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Yes!
Bubble Sort Algorithm
![Page 39: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/39.jpg)
39
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Swap?
Bubble Sort Algorithm
![Page 40: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/40.jpg)
40
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
Yes!
Bubble Sort Algorithm
![Page 41: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/41.jpg)
41
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Repeat.
[0] [1] [2] [3] [4] [5] Swap? No.
Bubble Sort Algorithm
![Page 42: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/42.jpg)
42
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Repeat.
[0] [1] [2] [3] [4] [5] Swap? No.
Bubble Sort Algorithm
![Page 43: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/43.jpg)
43
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Repeat.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 44: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/44.jpg)
44
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Repeat.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 45: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/45.jpg)
45
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Repeat.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 46: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/46.jpg)
46
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Repeat.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 47: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/47.jpg)
47
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Loop over array n-1 times, swapping pairs of entries as needed.
[0] [1] [2] [3] [4] [5] Swap? No.
Bubble Sort Algorithm
![Page 48: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/48.jpg)
48
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Loop over array n-1 times, swapping pairs of entries as needed.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 49: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/49.jpg)
49
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Loop over array n-1 times, swapping pairs of entries as needed.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 50: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/50.jpg)
50
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Loop over array n-1 times, swapping pairs of entries as needed.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 51: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/51.jpg)
51
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Loop over array n-1 times, swapping pairs of entries as needed.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 52: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/52.jpg)
52
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Continue looping, until done.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 53: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/53.jpg)
53
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Continue looping, until done.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 54: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/54.jpg)
54
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Continue looping, until done.
[0] [1] [2] [3] [4] [5] Swap? Yes.
Bubble Sort Algorithm
![Page 55: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/55.jpg)
55
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm - Result
![Page 56: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/56.jpg)
56
Bubble Sort Algorithmprocedure bubbleSort( A : list of sortable items ) n = length (A) repeat
swapped = false for i = 1 to n - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swappedend procedure
![Page 57: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/57.jpg)
57
Bubble Sort Algorithm - Optimized Can be easily optimized by observing that the n-th pass finds the n-th largest element and puts it into its final place
So, the inner loop can avoid looking at the last n-1 items when running for the n-th time:
procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) swapped = true end if end for n = n - 1 until not swappedend procedure
![Page 58: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/58.jpg)
58
Bubble Sort Algorithm – Optimized 2 More generally, it can happen that more than one element is placed in their final position on a single pass.
In particular, after every pass, all elements after the last swap are sorted, and do not need to be checked again. This allows us to skip over a lot of the elements, resulting in about a worst case 50% improvement in comparison count (though no improvement in swap counts), and adds very little complexity because the new code subsumes the "swapped" variable:
procedure bubbleSort( A : list of sortable items ) n = length(A) repeat newn = 0 for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) newn = i end if end for n = newn until n = 0end procedure
![Page 59: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/59.jpg)
59
Bubble Sort Implementation Codevoid bubbleSort (int list[ ] , int size) {
int i, j, temp; for ( i = 0; i < size; i++ ) { /* controls passes through the list */ for ( j = 0; j < size - 1; j++ ) /* performs adjacent comparisons
*/ { if ( list[ j ] > list[ j+1 ] ) /* determines if a swap should occur */
{ temp = list[ j ]; /* swap is performed */ list[ j ] = list[ j + 1 ]; list[ j+1 ] = temp;} // end of if statement
} // end of inner for loop} // end of outer for loop
} // end of function
![Page 60: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/60.jpg)
60
Bubble Sort Using Call-by-reference Implement bubblesort using pointers Swap two elements swap function must receive address (using &) of array
elements Array elements have call-by-value default
Using pointers and the * operator, swap can switch array elements
PsuedocodeInitialize array print data in original orderCall function bubblesort
print sorted arrayDefine bubblesort and Swap functions
![Page 61: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/61.jpg)
1. Initialize array
1.1 Declare variables
2. Print array
2.1 Call bubbleSort
2.2 Print array
1 /* Fig. 7.15: fig07_15.c2 This program puts values into an array, sorts the values into3 ascending order, and prints the resulting array. */4 #include <stdio.h>5 #define SIZE 106 void bubbleSort( int *, const int );78 int main()9 {10 11 int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };12 int i;1314 printf( "Data items in original order\n" );1516 for ( i = 0; i < SIZE; i++ )17 printf( "%4d", a[ i ] );1819 bubbleSort( a, SIZE ); /* sort the array */20 printf( "\nData items in ascending order\n" );2122 for ( i = 0; i < SIZE; i++ )23 printf( "%4d", a[ i ] ); 2425 printf( "\n" );2627 return 0;28 }29
Bubblesort gets passed the address of array elements (pointers). The name of an array is a pointer.
![Page 62: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/62.jpg)
3. Function definitions
Program Output
33 int pass, j; 34 for ( pass = 0; pass < size - 1; pass++ )3536 for ( j = 0; j < size - 1; j++ )
3738 if ( array[ j ] > array[ j + 1 ] )
39 swap( &array[ j ], &array[ j + 1 ] );40 }4142 void swap( int *element1Ptr, int *element2Ptr )43 {44 int hold = *element1Ptr;45 *element1Ptr = *element2Ptr;46 *element2Ptr = hold;47 }
Data items in original order 2 6 4 8 10 12 89 68 45 37Data items in ascending order 2 4 6 8 10 12 37 45 68 89
30 void bubbleSort(int *array, const int size)31{32 void swap( int *, int * );
![Page 63: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/63.jpg)
63
Bubble Sort
![Page 64: CSC 211 Data Structures Lecture 14](https://reader035.vdocuments.net/reader035/viewer/2022062301/56816248550346895dd287f2/html5/thumbnails/64.jpg)
64
Summary Sorting
Concept Reasons for Sorting Basic Terminology Sorting Classification Stability of Key Bubble Sort
Concept Algorithm Code and Implementation