SlideShare a Scribd company logo
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

More Related Content

Similar to Data structure.pptx

Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
ssuserd602fd
 
my docoment
my docomentmy docoment
my docoment
NeeshanYonzan
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
SwatiHans10
 
Sorting
SortingSorting
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
Dabbal Singh Mahara
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 
Searching
SearchingSearching
Searching
A. S. M. Shafi
 
searching
searchingsearching
searching
A. S. M. Shafi
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
sheraz7288
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Sorting
SortingSorting
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
18Gunaalanpg
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
amberkhan59
 

Similar to Data structure.pptx (20)

Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
my docoment
my docomentmy docoment
my docoment
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Sorting
SortingSorting
Sorting
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Searching
SearchingSearching
Searching
 
searching
searchingsearching
searching
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Sorting
SortingSorting
Sorting
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
 

More from SajalFayyaz

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
SajalFayyaz
 
data structure 9.pptx
data structure 9.pptxdata structure 9.pptx
data structure 9.pptx
SajalFayyaz
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
SajalFayyaz
 
Data structure 6.pptx
Data structure 6.pptxData structure 6.pptx
Data structure 6.pptx
SajalFayyaz
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
SajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
SajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
SajalFayyaz
 

More from SajalFayyaz (10)

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
data structure 9.pptx
data structure 9.pptxdata structure 9.pptx
data structure 9.pptx
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Data structure 6.pptx
Data structure 6.pptxData structure 6.pptx
Data structure 6.pptx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 

Recently uploaded

06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
Timothy Spann
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
Vietnam Cotton & Spinning Association
 
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
hqfek
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
AlessioFois2
 
Jio cinema Retention & Engagement Strategy.pdf
Jio cinema Retention & Engagement Strategy.pdfJio cinema Retention & Engagement Strategy.pdf
Jio cinema Retention & Engagement Strategy.pdf
inaya7568
 
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
hyfjgavov
 
How To Control IO Usage using Resource Manager
How To Control IO Usage using Resource ManagerHow To Control IO Usage using Resource Manager
How To Control IO Usage using Resource Manager
Alireza Kamrani
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
Building a Quantum Computer Neutral Atom.pdf
Building a Quantum Computer Neutral Atom.pdfBuilding a Quantum Computer Neutral Atom.pdf
Building a Quantum Computer Neutral Atom.pdf
cjimenez2581
 
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Kaxil Naik
 
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
taqyea
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
UofT毕业证如何办理
UofT毕业证如何办理UofT毕业证如何办理
UofT毕业证如何办理
exukyp
 
社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .
NABLAS株式会社
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
VyNguyen709676
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Kiwi Creative
 
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
nyvan3
 
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理 原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
tzu5xla
 
一比一原版美国帕森斯设计学院毕业证(parsons毕业证书)如何办理
一比一原版美国帕森斯设计学院毕业证(parsons毕业证书)如何办理一比一原版美国帕森斯设计学院毕业证(parsons毕业证书)如何办理
一比一原版美国帕森斯设计学院毕业证(parsons毕业证书)如何办理
asyed10
 

Recently uploaded (20)

06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
 
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
 
Jio cinema Retention & Engagement Strategy.pdf
Jio cinema Retention & Engagement Strategy.pdfJio cinema Retention & Engagement Strategy.pdf
Jio cinema Retention & Engagement Strategy.pdf
 
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
 
How To Control IO Usage using Resource Manager
How To Control IO Usage using Resource ManagerHow To Control IO Usage using Resource Manager
How To Control IO Usage using Resource Manager
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
Building a Quantum Computer Neutral Atom.pdf
Building a Quantum Computer Neutral Atom.pdfBuilding a Quantum Computer Neutral Atom.pdf
Building a Quantum Computer Neutral Atom.pdf
 
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
 
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
UofT毕业证如何办理
UofT毕业证如何办理UofT毕业证如何办理
UofT毕业证如何办理
 
社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
 
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
 
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理 原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
 
一比一原版美国帕森斯设计学院毕业证(parsons毕业证书)如何办理
一比一原版美国帕森斯设计学院毕业证(parsons毕业证书)如何办理一比一原版美国帕森斯设计学院毕业证(parsons毕业证书)如何办理
一比一原版美国帕森斯设计学院毕业证(parsons毕业证书)如何办理
 

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
  • 4. 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
  • 5. 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
  • 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 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
  • 8. 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
  • 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 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
  • 14. 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
  • 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. Selection sort process … Round 1
  • 27. Selection sort process … Round 2
  • 28. Selection sort process … Round 3
  • 29. Selection sort process … Round 4
  • 30. Selection sort process … Round 5
  • 31. Selection sort process … Round 5
  • 32. Selection sort process … Round 6
  • 33. Selection sort process … Round 7
  • 34. Selection sort process … Round 8
  • 35. Sorting Algorithms …….. Selection Sort Execute it on board for sample data:
  • 36. Selection sort sort process … 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