CS261
DATA STRUCTURES & ALGORITHMS
(WEEK-7)
LECTURE-13 & 14
INTRODUCTION TO DATA STRUCTURES &
ALGORITHMS
Lecturer
Azka Aziz
Azka.a@scocs.edu.pk
Data structures & Algorithms
Lecture#13 Sorting
Lecture contents
Sorting
Insertion Sort
Selection Sort
Bubble Sort
Sorting
Arranging elements of a list / set in some particular order based
on some criteria
Purpose of Sorting
Bringing set/list elements in some ordered / sequence
Categorize elements
Group elements with similar characteristics
Making searching process very efficient
Making merging of sorted elements very efficient
Typical Sorting orders
Ascending Order
Arranging elements of a list starting from smallest value to largest value e.g. A
to Z, 0 to 9
Descending Order
Opposite to asceinding order. It arranges elements of a list starting from
largest value to smallest value e.g. Z to A, 9 to 0
Sorting Algorithms …….. Insertion Sort
Say an array (to be sorted) has N elements
We split array into two parts; first part contains sorted elements while
second part contains elements that are not sorted yet
Initially the sorted part contains only one element (as it is sorted obviously)
while second part contains N-1 elements
Sorted Elements Elements not sorted yet
3 4 6 22 98 35 34 18 11 65
Insertion sort process
Insertion sort is an iterative process
During every iteration one element (immediately next to sorted part) is taken
from unsorted part and is inserted into sorted part such a way that resultant
sorted part remains sorted with one more elements into it
Sorted Elements Elements not sorted yet
3 4 6 22 98 35 34 18 11 65
Sorted Elements Elements not sorted yet
3 4 6 22 98 35 34 18 11 65
Insertion sort process
We keep iterating until that part of list containing unsorted elements keeps
no element at all
At this point resultant list is sorted
6 4 3 22 98 35 34 18 11 65
4 6 3 22 98 35 34 18 11 65
3 4 6 11 18 22 34 35 65 98
3 4 6 22 98 35 34 18 11 65
Insertion sort … animation
animation url: https://en.wikipedia.org/wiki/File:Insertion-sort-example-300px.gif
Sorting Algorithms …….. Insertion Sort
Execute it on
board for sample
data:
Insertion Sort … C++ Code
int data[MAX]={10, 25, 90, 5, 61, 44, 82, 72, 38,
59};
int n = MAX-1, key, j, i;
for(i=1; i<MAX; i++){
key = data[i];
j = i-1;
while(j>=0 && data[j] > key){
data[j+1]=data[j]; j--;
}
data[j+1]=key;
}
for(i=0; i<MAX;i++){
std::cout<<data[i]<<",";
}
Execute it on board for
given data:
Insertion sort … characteristics
Simple algorithm
Efficient for small data set
Stable sort (does not swap identical elements)
Requires constant external memory (memory beyond data array)
For data already sorted / almost sorted array requires O(N) time
For reverse sorted array requires O(N2) time
Bubble sort
Say an array (to be sorted) has N elements
Starting from bottom of list (Nth element of data list) every element is
compared with its neighboring element (immediately above it) sequentially
After comparison whichever element is smaller, moves up and other moves
down (like lighter air bubbles in water)
When one complete round is over, smallest element is at the top of the list
Bubble sort
After one round is over, we start again from bottom of the list and repeat the
process
Now when N-1 rounds are over, all list elements are sorted
Bubble sort process … one complete round
12
43
71
20
3
36
24
12
43
71
20
3
24
36
12
43
71
20
3
24
36
12
43
71
3
20
24
36
12
43
3
71
20
24
36
12
3
43
71
20
24
36
3
12
43
71
20
24
36
At the end of complete round, only top element is guaranteed to be the
smallest (ascending sort)
We have to start over from bottom for another round to get second smallest
value at its right place
Bubble sort process … round 2
3
12
43
71
20
24
36
3
12
43
71
20
24
36
3
12
43
71
20
24
36
3
12
43
20
71
24
36
3
12
20
43
71
24
36
3
12
20
43
71
24
36
Bubble sort process … round 3
3
12
20
43
71
24
36
3
12
20
43
71
24
36
3
12
20
43
24
71
36
3
12
20
24
43
71
36
3
12
20
24
43
71
36
Bubble sort process … round 4
3
12
20
24
43
71
36
3
12
20
24
43
36
71
3
12
20
24
36
43
71
3
12
20
24
36
43
71
Bubble sort process … round 5
3
12
20
24
36
43
71
3
12
20
24
36
43
71
3
12
20
24
36
43
71
Bubble sort process … round 6
3
12
20
24
36
43
71
3
12
20
24
36
43
71
Final outcome:
Sorted Array
Bubble sort … algorithm
Bubble sort process … Source code
Code it yourself
Bubble sort … characteristics
Simple algorithm
Very popular algorithm (very inefficient though)
Easier to implement
Compares adjacent values only and swaps if necessary (if values are not in
order)
It is a slow sorting algorithm requiring O(N2) steps to sort an array of values
Data structures & Algorithms
Lecture#14 Sorting(continue)
Selection Sort … The process
1. We have N elements in the list
2. Find out the smallest value (ascending order) from the list and replace it
with 1st element in the list
3. Step 2 keeps the smallest value to its appropriate position while leaving the
unsorted list’s size to one less than actual size
4. We can keep repeating steps 2 & 3 until unsorted list size reduces to 1. At
that point, whole list is sorted
Selection sort process … Round 1
Selection sort process … Round 2
Selection sort process … Round 3
Selection sort process … Round 4
Selection sort process … Round 5
Selection sort process … Round 5
Selection sort process … Round 6
Selection sort process … Round 7
Selection sort process … Round 8
Sorting Algorithms …….. Selection Sort
Execute it on board for
sample data:
Selection sort sort process … Source code
Code it yourself
Selection sort … characteristics
Simple sorting technique
It is called Selection Sort as it repeatedly selects the smallest/largest in a sub-
array of unsorted elements
An item at moves only once
Method of choice for files containing very large records and small keys
It is a slow sorting algorithm requiring O(N2) steps to sort an array of values in
worst case
Sorting Algorithms ….. Videos
1. Selection Sort
https://www.youtube.com/watch?v=f8hXR_Hvybo
https://www.youtube.com/watch?v=lx9G71uLXIg
2. Insertion Sort
https://www.youtube.com/watch?v=DFG-XuyPYUQ
https://www.youtube.com/watch?v=TwGb6ohsvUU
3. Bubble Sort
https://www.youtube.com/watch?v=Ui97-_n5xjo
https://www.youtube.com/watch?v=Ui97-_n5xjo

Data structure.pptx

  • 1.
    CS261 DATA STRUCTURES &ALGORITHMS (WEEK-7) LECTURE-13 & 14 INTRODUCTION TO DATA STRUCTURES & ALGORITHMS Lecturer Azka Aziz Azka.a@scocs.edu.pk
  • 2.
    Data structures &Algorithms Lecture#13 Sorting
  • 3.
  • 4.
    Sorting Arranging elements ofa list / set in some particular order based on some criteria Purpose of Sorting Bringing set/list elements in some ordered / sequence Categorize elements Group elements with similar characteristics Making searching process very efficient Making merging of sorted elements very efficient
  • 5.
    Typical Sorting orders AscendingOrder Arranging elements of a list starting from smallest value to largest value e.g. A to Z, 0 to 9 Descending Order Opposite to asceinding order. It arranges elements of a list starting from largest value to smallest value e.g. Z to A, 9 to 0
  • 6.
    Sorting Algorithms ……..Insertion Sort Say an array (to be sorted) has N elements We split array into two parts; first part contains sorted elements while second part contains elements that are not sorted yet Initially the sorted part contains only one element (as it is sorted obviously) while second part contains N-1 elements Sorted Elements Elements not sorted yet 3 4 6 22 98 35 34 18 11 65
  • 7.
    Insertion sort process Insertionsort is an iterative process During every iteration one element (immediately next to sorted part) is taken from unsorted part and is inserted into sorted part such a way that resultant sorted part remains sorted with one more elements into it Sorted Elements Elements not sorted yet 3 4 6 22 98 35 34 18 11 65 Sorted Elements Elements not sorted yet 3 4 6 22 98 35 34 18 11 65
  • 8.
    Insertion sort process Wekeep iterating until that part of list containing unsorted elements keeps no element at all At this point resultant list is sorted 6 4 3 22 98 35 34 18 11 65 4 6 3 22 98 35 34 18 11 65 3 4 6 11 18 22 34 35 65 98 3 4 6 22 98 35 34 18 11 65
  • 9.
    Insertion sort …animation animation url: https://en.wikipedia.org/wiki/File:Insertion-sort-example-300px.gif
  • 10.
    Sorting Algorithms ……..Insertion Sort Execute it on board for sample data:
  • 11.
    Insertion Sort …C++ Code int data[MAX]={10, 25, 90, 5, 61, 44, 82, 72, 38, 59}; int n = MAX-1, key, j, i; for(i=1; i<MAX; i++){ key = data[i]; j = i-1; while(j>=0 && data[j] > key){ data[j+1]=data[j]; j--; } data[j+1]=key; } for(i=0; i<MAX;i++){ std::cout<<data[i]<<","; } Execute it on board for given data:
  • 12.
    Insertion sort …characteristics Simple algorithm Efficient for small data set Stable sort (does not swap identical elements) Requires constant external memory (memory beyond data array) For data already sorted / almost sorted array requires O(N) time For reverse sorted array requires O(N2) time
  • 13.
    Bubble sort Say anarray (to be sorted) has N elements Starting from bottom of list (Nth element of data list) every element is compared with its neighboring element (immediately above it) sequentially After comparison whichever element is smaller, moves up and other moves down (like lighter air bubbles in water) When one complete round is over, smallest element is at the top of the list
  • 14.
    Bubble sort After oneround is over, we start again from bottom of the list and repeat the process Now when N-1 rounds are over, all list elements are sorted
  • 15.
    Bubble sort process… one complete round 12 43 71 20 3 36 24 12 43 71 20 3 24 36 12 43 71 20 3 24 36 12 43 71 3 20 24 36 12 43 3 71 20 24 36 12 3 43 71 20 24 36 3 12 43 71 20 24 36 At the end of complete round, only top element is guaranteed to be the smallest (ascending sort) We have to start over from bottom for another round to get second smallest value at its right place
  • 16.
    Bubble sort process… round 2 3 12 43 71 20 24 36 3 12 43 71 20 24 36 3 12 43 71 20 24 36 3 12 43 20 71 24 36 3 12 20 43 71 24 36 3 12 20 43 71 24 36
  • 17.
    Bubble sort process… round 3 3 12 20 43 71 24 36 3 12 20 43 71 24 36 3 12 20 43 24 71 36 3 12 20 24 43 71 36 3 12 20 24 43 71 36
  • 18.
    Bubble sort process… round 4 3 12 20 24 43 71 36 3 12 20 24 43 36 71 3 12 20 24 36 43 71 3 12 20 24 36 43 71
  • 19.
    Bubble sort process… round 5 3 12 20 24 36 43 71 3 12 20 24 36 43 71 3 12 20 24 36 43 71
  • 20.
    Bubble sort process… round 6 3 12 20 24 36 43 71 3 12 20 24 36 43 71 Final outcome: Sorted Array
  • 21.
    Bubble sort …algorithm
  • 22.
    Bubble sort process… Source code Code it yourself
  • 23.
    Bubble sort …characteristics Simple algorithm Very popular algorithm (very inefficient though) Easier to implement Compares adjacent values only and swaps if necessary (if values are not in order) It is a slow sorting algorithm requiring O(N2) steps to sort an array of values
  • 24.
    Data structures &Algorithms Lecture#14 Sorting(continue)
  • 25.
    Selection Sort …The process 1. We have N elements in the list 2. Find out the smallest value (ascending order) from the list and replace it with 1st element in the list 3. Step 2 keeps the smallest value to its appropriate position while leaving the unsorted list’s size to one less than actual size 4. We can keep repeating steps 2 & 3 until unsorted list size reduces to 1. At that point, whole list is sorted
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
    Sorting Algorithms ……..Selection Sort Execute it on board for sample data:
  • 36.
    Selection sort sortprocess … Source code Code it yourself
  • 37.
    Selection sort …characteristics Simple sorting technique It is called Selection Sort as it repeatedly selects the smallest/largest in a sub- array of unsorted elements An item at moves only once Method of choice for files containing very large records and small keys It is a slow sorting algorithm requiring O(N2) steps to sort an array of values in worst case
  • 38.
    Sorting Algorithms …..Videos 1. Selection Sort https://www.youtube.com/watch?v=f8hXR_Hvybo https://www.youtube.com/watch?v=lx9G71uLXIg 2. Insertion Sort https://www.youtube.com/watch?v=DFG-XuyPYUQ https://www.youtube.com/watch?v=TwGb6ohsvUU 3. Bubble Sort https://www.youtube.com/watch?v=Ui97-_n5xjo https://www.youtube.com/watch?v=Ui97-_n5xjo