SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Table of contents
1
Searching
Sequential / Linear Search 2
Binary Search 3
Sorting
Selection Sort 4
Radix Sort 5 – 8
Quick Sort 9 – 12
Insertion Sort 13 – 14
Bubble Sort 15 - 17
1. i = 0
2. Read value of key to be searched.
3. if k( i ) == key
Display "Recode found at position"
go to step 7
4. increment k
5. if i < n
go to step 3
6. Display "No match found!"
7. Stop
Algorithm of Sequential / Linear Search
2
Binary Search
1. Accept Key
2. Top = 0, Bottom = n-1
3. m=(Top + Bottom) / 2
4. if (key==k(mid))
Display Record found at position mid.
go to step 8
5. if key< k(mid)
else
top=m+1
6. if (top<=bottom)
go to step 3
7. Display Record Found
8. Stop
3
Algorithm of Selection Sort
1. Start
2. Pass = 1
3. i = 0
4. j = i+1
5. if x[i] > x[j]
interchange x[i] and x[i+1]
6. j = j+1
7. if j < = n-1
Go to step 5
8. Pass = Pass + 1
9. if pass < = n-1
then i = i+1 and go to step 4
10. Stop
4
Algorithm of Radix Sort
This method is based on the value of actual digits in each position of the numbers being sorted.
Example: 132 has 2 in the unit's position , 3 in the ten's position and 1 in the hundred's position.
Start comparing from the most significant digits and move towards the last digit as long as the
digits are equal. if there is no match, the number with digit is the greater number.
This method used the above techniques.
1. The number are partitioned into ten groups based on their unit's digit.
2. Elements within individual groups are sorted on ten's digit.
3. The process is repeated till each sub group has been subdivided an most significant digit.
in the above method, a lot of partitions have to be created which complexity.
Comparing two numbers of equal length
5
Inputs Packets
0 1 2 3 4 5 6 7 8 9
612 612
912 912
796 796
412 412
432 432
100 100
206 206
First Pass
Example of Radix Sort
Example: 612, 912, 796, 412, 432, 100, 206
6
Inputs Packets
0 1 2 3 4 5 6 7 8 9
100 100
612 612
912 912
412 412
432 432
796 796
206 206
Second Pass
7
Inputs Packets
0 1 2 3 4 5 6 7 8 9
100 100
206 206
612 612
912 912
412 412
432 432
796 796
Third Pass
Sorted list : 100, 206, 412, 432, 612, 796, 912
8
1. Start
2. A is an array of 'n' elements.
3. lb=0, ub = n-1 ( ub → upper bound, lb → lower bound)
4. if ( lb < ub ) i.e. if the array can be partitioned.
j = partition (A, lb, j-1) // j is the pivot position
Quicksort (A, lb, j-1) // sort the first partition
Quicksort (A, j+1, ub ) // sort the second partition
5. Stop
Algorithm of Quick Sort
9
Pivot position = up
First sub array = 23 7 48 32 18
Second partition = 82 62
[55] 7 48 32 18 23 82 62
Partition I Partition II
Pivot = A [lb] = 55
Move dn (82 is greater than pivot so stop
incrementing dn)
Move up (23 is less than pivot so stop
decrementing up)
Interchange A[dn] and A[up]
updn
Example of Quick Sort
[55] 7 48 32 18 23 82 62
[55] 7 48 32 18 23 82 62
23 7 48 32 18 [55] 82 62
Example : 55, 7, 48, 32, 18, 23, 82, 62
dn
dn
dn
up
up
up
10
1. down = lb
2. up = ub
3. pivot = A[lb]
4. While (A[down] < = pivot && down < ub)
down ++
5. while (A[up] > pivot && up > lb)
up --
6. if (down < up)
interchange A [down] and A [up]
go to step 4
7. interchange A[up] and pivot
8. return up
9. Stop
Algorithm of Quick Sort (Partitions)
11
[23] 7 48 32 18
[23] 7 48 32 18
[23] 7 48 32 18
[23] 7 18 32 48
[23] 7 18 32 48
18 7 [23] 32 48
[23] 7 18 32 48
Partition I Partition II
Pivot = A [lb] = 23
Move dn (48 is greater than pivot - 23
so stop incrementing dn)
Move up (18 is less than pivot - 23
so stop decrementing up)
Interchange A[dn] and A[up]
Move dn
Move up
Interchange A[up] and pivot
updn
dn
dn
dn
dn
dn
up
up
up
up
dn up
up
Example of Quick Sort (Partitions)
Example: 55, 7, 48, 32, 18, 23, 82, 62
12
Algorithm of Insertions Sort
• In the first pass the 1st element 7 is compared with 0th element 15, 7 is inserted
at 0th place, since 7 is smaller than 15. The 0th element. i.e. 15 is moved one
position to the right.
• In the second pass, the 2nd element 22 is compared with 0th element 7. hence 7
is smaller than 22 then no change. Then 2nd element 22 is compared with 1st
element 15 and again no change.
• In the third pass 3rd element is compared with 0th element. As 3 is smaller than
7, 3 is inserted at 0th place and all the elements from 0th till 2nd position are
moved to right by one position.
• This procedure is repeated for all the element of array. In the last pass array gets
sorted.
13
Example of Insertions Sort
15 7 22 3 14 2
7 15 22 3 14 2
7 15 22 3 14 2
7 15 22 3 14 2
3 7 15 22 14 2
3 7 15 22 14 2
3 7 15 22 14 2
3 7 15 22 14 2
3 7 15 22 14 2
3 7 14 15 22 2
3 7 14 15 22 2
2 3 7 14 15 22
First pass
Second pass
Third pass
Fifth pass
Sorted array
Fourth pass
14
Algorithm of Bubble Sort
1. Start
2. Pass = 1
3. i = 0
4. If x[i] > x(i + 1)
interchange x[i] and x[I + 1]
5. i = i + 1
6. If i<=n-1- Pass
go to Step 4
7. Pass =Pass + 1
8. If Pass < = n-1
go to Step 3
9. Stop
15
Example of Bubble Sort
Example: 25, 37, 12, 48, 57, 33
25 25 25 25 25 25
37 37 12 12 12 12
12 12 37 37 37 37
48 48 48 48 48 48
57 57 57 57 57 33
33 33 33 33 33 57
12 12 12 12 12
25 25 25 25 25
37 37 37 37 37
48 48 48 48 33
33 33 33 33 48
57 57 57 57 57
Pass 1 Pass 2
16
Example of Bubble Sort
Example: 25, 37, 12, 48, 57, 33
12 12 12 12
25 25 25 25
37 37 37 33
33 33 33 37
48 48 48 48
57 57 57 57
12 12 12
25 25 25
33 33 33
37 37 37
48 48 48
57 57 57
Pass 3 Pass 4
12 12
25 25
33 33
37 37
48 48
57 57
Pass 5
17

Weitere ähnliche Inhalte

Was ist angesagt?

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
 

Was ist angesagt? (19)

Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)
 
Queues-handouts
Queues-handoutsQueues-handouts
Queues-handouts
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
Queue
QueueQueue
Queue
 
Circular queue
Circular queueCircular queue
Circular queue
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Stack
StackStack
Stack
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Stack application
Stack applicationStack application
Stack application
 

Ähnlich wie Sorting and Searching - Data Structure - Notes

An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 

Ähnlich wie Sorting and Searching - Data Structure - Notes (20)

Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Lecture#9
Lecture#9Lecture#9
Lecture#9
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Data structure notes
Data structure notesData structure notes
Data structure notes
 

Mehr von Omprakash Chauhan

motherboard electronic components and their functions
motherboard electronic components and their functionsmotherboard electronic components and their functions
motherboard electronic components and their functions
Omprakash Chauhan
 
System of units
System of unitsSystem of units
System of units
Omprakash Chauhan
 

Mehr von Omprakash Chauhan (17)

Introduction to curve
Introduction to curveIntroduction to curve
Introduction to curve
 
Basic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - NotesBasic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - Notes
 
Polygons - Computer Graphics - Notes
Polygons - Computer Graphics - NotesPolygons - Computer Graphics - Notes
Polygons - Computer Graphics - Notes
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - Notes
 
Basic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - NotesBasic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - Notes
 
E-R Diagram of College Management Systems
E-R Diagram of College Management SystemsE-R Diagram of College Management Systems
E-R Diagram of College Management Systems
 
Burglar Alarm Micro Project
Burglar Alarm Micro ProjectBurglar Alarm Micro Project
Burglar Alarm Micro Project
 
Simple Calculator Flowchart
Simple Calculator FlowchartSimple Calculator Flowchart
Simple Calculator Flowchart
 
Full Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) MicroprojectFull Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) Microproject
 
A detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skillsA detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skills
 
Fractional-horsepower Motor Report
Fractional-horsepower Motor ReportFractional-horsepower Motor Report
Fractional-horsepower Motor Report
 
motherboard electronic components and their functions
motherboard electronic components and their functionsmotherboard electronic components and their functions
motherboard electronic components and their functions
 
How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.
 
Process of ionization
Process of ionizationProcess of ionization
Process of ionization
 
Welcome to the world of ionization
Welcome to the world of ionization Welcome to the world of ionization
Welcome to the world of ionization
 
Printer
PrinterPrinter
Printer
 
System of units
System of unitsSystem of units
System of units
 

Kürzlich hochgeladen

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Kürzlich hochgeladen (20)

Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 

Sorting and Searching - Data Structure - Notes

  • 1. Table of contents 1 Searching Sequential / Linear Search 2 Binary Search 3 Sorting Selection Sort 4 Radix Sort 5 – 8 Quick Sort 9 – 12 Insertion Sort 13 – 14 Bubble Sort 15 - 17
  • 2. 1. i = 0 2. Read value of key to be searched. 3. if k( i ) == key Display "Recode found at position" go to step 7 4. increment k 5. if i < n go to step 3 6. Display "No match found!" 7. Stop Algorithm of Sequential / Linear Search 2
  • 3. Binary Search 1. Accept Key 2. Top = 0, Bottom = n-1 3. m=(Top + Bottom) / 2 4. if (key==k(mid)) Display Record found at position mid. go to step 8 5. if key< k(mid) else top=m+1 6. if (top<=bottom) go to step 3 7. Display Record Found 8. Stop 3
  • 4. Algorithm of Selection Sort 1. Start 2. Pass = 1 3. i = 0 4. j = i+1 5. if x[i] > x[j] interchange x[i] and x[i+1] 6. j = j+1 7. if j < = n-1 Go to step 5 8. Pass = Pass + 1 9. if pass < = n-1 then i = i+1 and go to step 4 10. Stop 4
  • 5. Algorithm of Radix Sort This method is based on the value of actual digits in each position of the numbers being sorted. Example: 132 has 2 in the unit's position , 3 in the ten's position and 1 in the hundred's position. Start comparing from the most significant digits and move towards the last digit as long as the digits are equal. if there is no match, the number with digit is the greater number. This method used the above techniques. 1. The number are partitioned into ten groups based on their unit's digit. 2. Elements within individual groups are sorted on ten's digit. 3. The process is repeated till each sub group has been subdivided an most significant digit. in the above method, a lot of partitions have to be created which complexity. Comparing two numbers of equal length 5
  • 6. Inputs Packets 0 1 2 3 4 5 6 7 8 9 612 612 912 912 796 796 412 412 432 432 100 100 206 206 First Pass Example of Radix Sort Example: 612, 912, 796, 412, 432, 100, 206 6
  • 7. Inputs Packets 0 1 2 3 4 5 6 7 8 9 100 100 612 612 912 912 412 412 432 432 796 796 206 206 Second Pass 7
  • 8. Inputs Packets 0 1 2 3 4 5 6 7 8 9 100 100 206 206 612 612 912 912 412 412 432 432 796 796 Third Pass Sorted list : 100, 206, 412, 432, 612, 796, 912 8
  • 9. 1. Start 2. A is an array of 'n' elements. 3. lb=0, ub = n-1 ( ub → upper bound, lb → lower bound) 4. if ( lb < ub ) i.e. if the array can be partitioned. j = partition (A, lb, j-1) // j is the pivot position Quicksort (A, lb, j-1) // sort the first partition Quicksort (A, j+1, ub ) // sort the second partition 5. Stop Algorithm of Quick Sort 9
  • 10. Pivot position = up First sub array = 23 7 48 32 18 Second partition = 82 62 [55] 7 48 32 18 23 82 62 Partition I Partition II Pivot = A [lb] = 55 Move dn (82 is greater than pivot so stop incrementing dn) Move up (23 is less than pivot so stop decrementing up) Interchange A[dn] and A[up] updn Example of Quick Sort [55] 7 48 32 18 23 82 62 [55] 7 48 32 18 23 82 62 23 7 48 32 18 [55] 82 62 Example : 55, 7, 48, 32, 18, 23, 82, 62 dn dn dn up up up 10
  • 11. 1. down = lb 2. up = ub 3. pivot = A[lb] 4. While (A[down] < = pivot && down < ub) down ++ 5. while (A[up] > pivot && up > lb) up -- 6. if (down < up) interchange A [down] and A [up] go to step 4 7. interchange A[up] and pivot 8. return up 9. Stop Algorithm of Quick Sort (Partitions) 11
  • 12. [23] 7 48 32 18 [23] 7 48 32 18 [23] 7 48 32 18 [23] 7 18 32 48 [23] 7 18 32 48 18 7 [23] 32 48 [23] 7 18 32 48 Partition I Partition II Pivot = A [lb] = 23 Move dn (48 is greater than pivot - 23 so stop incrementing dn) Move up (18 is less than pivot - 23 so stop decrementing up) Interchange A[dn] and A[up] Move dn Move up Interchange A[up] and pivot updn dn dn dn dn dn up up up up dn up up Example of Quick Sort (Partitions) Example: 55, 7, 48, 32, 18, 23, 82, 62 12
  • 13. Algorithm of Insertions Sort • In the first pass the 1st element 7 is compared with 0th element 15, 7 is inserted at 0th place, since 7 is smaller than 15. The 0th element. i.e. 15 is moved one position to the right. • In the second pass, the 2nd element 22 is compared with 0th element 7. hence 7 is smaller than 22 then no change. Then 2nd element 22 is compared with 1st element 15 and again no change. • In the third pass 3rd element is compared with 0th element. As 3 is smaller than 7, 3 is inserted at 0th place and all the elements from 0th till 2nd position are moved to right by one position. • This procedure is repeated for all the element of array. In the last pass array gets sorted. 13
  • 14. Example of Insertions Sort 15 7 22 3 14 2 7 15 22 3 14 2 7 15 22 3 14 2 7 15 22 3 14 2 3 7 15 22 14 2 3 7 15 22 14 2 3 7 15 22 14 2 3 7 15 22 14 2 3 7 15 22 14 2 3 7 14 15 22 2 3 7 14 15 22 2 2 3 7 14 15 22 First pass Second pass Third pass Fifth pass Sorted array Fourth pass 14
  • 15. Algorithm of Bubble Sort 1. Start 2. Pass = 1 3. i = 0 4. If x[i] > x(i + 1) interchange x[i] and x[I + 1] 5. i = i + 1 6. If i<=n-1- Pass go to Step 4 7. Pass =Pass + 1 8. If Pass < = n-1 go to Step 3 9. Stop 15
  • 16. Example of Bubble Sort Example: 25, 37, 12, 48, 57, 33 25 25 25 25 25 25 37 37 12 12 12 12 12 12 37 37 37 37 48 48 48 48 48 48 57 57 57 57 57 33 33 33 33 33 33 57 12 12 12 12 12 25 25 25 25 25 37 37 37 37 37 48 48 48 48 33 33 33 33 33 48 57 57 57 57 57 Pass 1 Pass 2 16
  • 17. Example of Bubble Sort Example: 25, 37, 12, 48, 57, 33 12 12 12 12 25 25 25 25 37 37 37 33 33 33 33 37 48 48 48 48 57 57 57 57 12 12 12 25 25 25 33 33 33 37 37 37 48 48 48 57 57 57 Pass 3 Pass 4 12 12 25 25 33 33 37 37 48 48 57 57 Pass 5 17