SlideShare ist ein Scribd-Unternehmen logo
1 von 17
SEARCHING & SORTING
TECHNIQUES
By
Kaushal Shah
LINEAR/SEQUENTIAL
SEARCH
 The simplest technique for searching an
unordered array for a particular element is to
scan each entry in the array in a sequential
manner until the desired element is found.
ALGORITHM:
SEQSEARCH (K, N, X) – Given an unordered array K of N
elements, this algorithm searches the array for a particular
element having value X. The given algorithm returns the index
(position) of the required element if the search is successful,
and returns 0 otherwise.
 
1. [Initialize search]
I  1
2. [Search the array]
Repeat while K[I] ≠ X
I  I + 1
3. [Is the search successful?]
if I > N
then Write (‘UNSUCCESSFUL SEARCH’)
Return (0)
else Write (‘SUCCESSFUL SEARCH’)
Return (I)
BINARY SEARCH
 In this method, the main requirement is
that the array should be arranged in
ascending order.
 Here, the approximate middle element of
the array is located, and its value is
examined.
If its value is too high, then the value of the
middle element of the first half of the array is
examined and the procedure is repeated to the
first half until the required item is found.
If the value is too low, then the value of the
middle element of the second half of the
array is checked and the procedure is repeated.
BINARYSEARCH (K, N, X) – Given an array K of size N arranged in
ascending order, this algorithm searches the array for a given
element whose value is given by X. The variables LOW, MIDDLE and
HIGH denote the lower, middle and upper limits of the search
interval, respectively.
 1. [Initialize]
LOW  1
HIGH  N
2. [Perform search]
Repeat thru step 4 while LOW <= HIGH
3. [Obtain index of midpoint of interval]
MIDDLE  (LOW + HIGH) / 2
4. [Compare]
If X < K [MIDDLE]
then HIGH  MIDDLE – 1
else if X > K[MIDDLE]
then LOW  MIDDLE + 1
else Write (‘SUCCESSFUL SEARCH’)
Return (MIDDLE)
5. [Unsuccessful Search]
Write (‘UNSUCCESSFUL SEARCH’)
Return (0)
BINARY SEARCH: TRACING
A trace of binary search algorithm for the sample array: 
75, 151, 203, 275, 318, 489, 524, 591, 647, and 727
 is given below for X = 275 and X = 727…
Search for X = 275
Iteration LOW HIGH MIDDLE
1 1 10 5
2 1 4 2
3 3 4 3
4 4 4 4
Search for X = 727
Iteration LOW HIGH MIDDLE
1 1 10 5
2 6 10 8
3 9 10 9
4 10 10 10
SELECTION SORT
 One of the easiest ways to sort an array is by
selection.
 In this technique, the smallest element is
interchanged with the first element of the
array.
 Then the second smallest element is
interchanged with the second element of the
array.
 This process of searching the next smallest element
and placing it in its proper position continues until
all records have been sorted in ascending order.
TRACIN
G
 Here a pass is defined as the search for the next smallest
element. To perform the sort operation, this technique will
require n-1 passes.
 In the above figure, each “encircled” entry denotes the
record with the smallest element selected in a particular
pass. The elements above the “double underline” for a
given pass are those elements that have been placed in order.
SELECTIONSORT (K, N) – Given an array K of size N, this procedure
rearranges the elements of array in ascending order. The variable
PASS represents the pass index and the position of the first element in
the array which is to be examined during a particular pass. The
variable MIN_INDEX shows the position of the smallest element
encountered till now in a particular pass. The variable I is used to
index elements from PASS to N in a given pass.
 1. [Create a loop for n-1 passes]
Repeat thru step 4 for PASS = 1, 2, …, N - 1
2. [Initialize MIN_INDEX to first element of each pass]
MIN_INDEX  PASS
3. [Make a pass and obtain element with smallest value]
Repeat for I = PASS + 1, PASS + 2, …, N
If K[I[ < K[MIN_INDEX]
Then MIN_INDEX  I
4. [Exchange smallest element with first element of each pass]
If MIN_INDEX ≠ PASS
Then K[PASS] ↔ K[MIN_INDEX] (Interchange)
5. [Finished]
Return
BUBBLE SORT
 In this technique, two elements of an array are
interchanged immediately upon discovering
that they are out of order.
 During the first pass, the first and second
elements are compared, say R1 and R2, and if they
are out of order (i.e. R1>R2), then they are
interchanged; this process is repeated for second
and third elements (R2 and R3), then for third and
fourth element (R3 and R4), and so on.
 After first pass, the largest element will be in the
last position.
 This process of comparing consecutive elements
continues until the whole array is sorted.
 Note: The whole process requires at the most n-1
passes. After each pass through the array, a check can
be made to determine whether any interchange
was made during that pass. If no interchange
occurred in a particular pass, then the array must be
sorted and no further passes are required.
TRACIN
G
BUBBLESORT (K, N) – Given an array K of size N, this procedure rearranges the
elements of array in ascending order. PASS and LAST variables are used for the
pass counter and position of the last unsorted element, respectively. The variable I
is used to index the elements of array. The variable EXCHS is used to count the
number of interchanges made on any pass.
1. [Initialize]
LAST  N
2. [Create a loop for the passes]
Repeat thru step 5 for PASS = 1, 2, …, N - 1
3. [Initialize counter for no of interchanges for this pass]
EXCHS  0
4. [Perform pair-wise comparisons on unsorted consecutive elements]
Repeat for I = 1, 2, … LAST-1
If K[I[ > K[I+1]
Then K[I] ↔ K[I+1] (Interchange elements)
EXCHS  EXCHS + 1
5. [Check if any interchange was made on this pass]
If EXCHS = 0
Then Return (Array Sorted; Return early)
Else
LAST  LAST – 1 (Reduce size of unsorted list)
6. [Finished]
Return
INSERTION SORT
 Insertion sort is a simple sorting algorithm: a
comparison sort in which the sorted array (or list)
is built one entry at a time. It is much less efficient
on large lists than more advanced algorithms such
as quicksort, or merge sort.
 Every repetition of insertion sort removes an
element from the input data, inserting it into the
correct position in the already-sorted list, until no
input elements remain.
 Sorting is typically done in-place. The resulting
array after k iterations has the property where the
first k + 1 entries are sorted. In each iteration the
first remaining entry of the input is removed,
inserted into the result at the correct position, thus
extending the result:
BEFORE
AFTER
InsSort(A[1…N]) –
For i = 2 to N
{
temp = A[i]
j = i-1
while(j>0 and temp<A[j])
{
A[j+1] = A[j]
j = j-1
}
A[j+1] = temp
}
ALGORITHM:
Unsorted Array: 3, 7, 4, 9, 5, 2, 6, 1
i temp j
1 7 0 3 7 4 9 5 2 6 1
2 4 1 3 7 7 9 5 2 6 1
3 4 7 9 5 2 6 10
3 9 2 3 4 7 9 5 2 6 1
4 5 3 3 4 7 9 9 2 6 1
3 4 7 7 9 2 6 1
3 4 5 7 9 2 6 1
2
1
5 2 4 3 4 5 7 9 9 6 1
3 4 5 7 7 9 6 1
3 4 5 5 7 9 6 1
3 4 4 5 7 9 6 1
3 3 4 5 7 9 6 1
2 3 4 5 7 9 6 1
3
2
1
0
-1
6 6 5 2 3 4 5 7 9 9 1
2 3 4 5 7 7 9 1
2 3 4 5 6 7 9 1
4
3
7 1 6 2 3 4 5 6 7 9 9
2 3 4 5 6 7 7 9
2 3 4 5 6 6 7 9
2 3 4 5 5 6 7 9
2 3 4 4 5 6 7 9
2 3 3 4 5 6 7 9
2 2 3 4 5 6 7 9
5
4
3
2
1
0
-1

Weitere ähnliche Inhalte

Was ist angesagt?

Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear searchmontazur420
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureTushar Gonawala
 
Searching in c language
Searching in c languageSearching in c language
Searching in c languageCHANDAN KUMAR
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Linear Search
Linear SearchLinear Search
Linear SearchSWATHIR72
 

Was ist angesagt? (20)

Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Array data structure
Array data structureArray data structure
Array data structure
 
Stack project
Stack projectStack project
Stack project
 
Data structures
Data structuresData structures
Data structures
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
Selection sort
Selection sortSelection sort
Selection sort
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Quick sort
Quick sortQuick sort
Quick sort
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Searching
SearchingSearching
Searching
 
Linear Search
Linear SearchLinear Search
Linear Search
 

Andere mochten auch

Andere mochten auch (6)

Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
Sorting
SortingSorting
Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 

Ähnlich wie Data Structures - Searching & sorting

Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14sumitbardhan
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannualmaamir farooq
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)eShikshak
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sortingFadhil Ismail
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 

Ähnlich wie Data Structures - Searching & sorting (20)

sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Sorting
SortingSorting
Sorting
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannual
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 

Kürzlich hochgeladen

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 

Kürzlich hochgeladen (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 

Data Structures - Searching & sorting

  • 2. LINEAR/SEQUENTIAL SEARCH  The simplest technique for searching an unordered array for a particular element is to scan each entry in the array in a sequential manner until the desired element is found.
  • 3. ALGORITHM: SEQSEARCH (K, N, X) – Given an unordered array K of N elements, this algorithm searches the array for a particular element having value X. The given algorithm returns the index (position) of the required element if the search is successful, and returns 0 otherwise.   1. [Initialize search] I  1 2. [Search the array] Repeat while K[I] ≠ X I  I + 1 3. [Is the search successful?] if I > N then Write (‘UNSUCCESSFUL SEARCH’) Return (0) else Write (‘SUCCESSFUL SEARCH’) Return (I)
  • 4. BINARY SEARCH  In this method, the main requirement is that the array should be arranged in ascending order.  Here, the approximate middle element of the array is located, and its value is examined. If its value is too high, then the value of the middle element of the first half of the array is examined and the procedure is repeated to the first half until the required item is found. If the value is too low, then the value of the middle element of the second half of the array is checked and the procedure is repeated.
  • 5. BINARYSEARCH (K, N, X) – Given an array K of size N arranged in ascending order, this algorithm searches the array for a given element whose value is given by X. The variables LOW, MIDDLE and HIGH denote the lower, middle and upper limits of the search interval, respectively.  1. [Initialize] LOW  1 HIGH  N 2. [Perform search] Repeat thru step 4 while LOW <= HIGH 3. [Obtain index of midpoint of interval] MIDDLE  (LOW + HIGH) / 2 4. [Compare] If X < K [MIDDLE] then HIGH  MIDDLE – 1 else if X > K[MIDDLE] then LOW  MIDDLE + 1 else Write (‘SUCCESSFUL SEARCH’) Return (MIDDLE) 5. [Unsuccessful Search] Write (‘UNSUCCESSFUL SEARCH’) Return (0)
  • 6. BINARY SEARCH: TRACING A trace of binary search algorithm for the sample array:  75, 151, 203, 275, 318, 489, 524, 591, 647, and 727  is given below for X = 275 and X = 727… Search for X = 275 Iteration LOW HIGH MIDDLE 1 1 10 5 2 1 4 2 3 3 4 3 4 4 4 4 Search for X = 727 Iteration LOW HIGH MIDDLE 1 1 10 5 2 6 10 8 3 9 10 9 4 10 10 10
  • 7. SELECTION SORT  One of the easiest ways to sort an array is by selection.  In this technique, the smallest element is interchanged with the first element of the array.  Then the second smallest element is interchanged with the second element of the array.  This process of searching the next smallest element and placing it in its proper position continues until all records have been sorted in ascending order.
  • 8. TRACIN G  Here a pass is defined as the search for the next smallest element. To perform the sort operation, this technique will require n-1 passes.  In the above figure, each “encircled” entry denotes the record with the smallest element selected in a particular pass. The elements above the “double underline” for a given pass are those elements that have been placed in order.
  • 9. SELECTIONSORT (K, N) – Given an array K of size N, this procedure rearranges the elements of array in ascending order. The variable PASS represents the pass index and the position of the first element in the array which is to be examined during a particular pass. The variable MIN_INDEX shows the position of the smallest element encountered till now in a particular pass. The variable I is used to index elements from PASS to N in a given pass.  1. [Create a loop for n-1 passes] Repeat thru step 4 for PASS = 1, 2, …, N - 1 2. [Initialize MIN_INDEX to first element of each pass] MIN_INDEX  PASS 3. [Make a pass and obtain element with smallest value] Repeat for I = PASS + 1, PASS + 2, …, N If K[I[ < K[MIN_INDEX] Then MIN_INDEX  I 4. [Exchange smallest element with first element of each pass] If MIN_INDEX ≠ PASS Then K[PASS] ↔ K[MIN_INDEX] (Interchange) 5. [Finished] Return
  • 10. BUBBLE SORT  In this technique, two elements of an array are interchanged immediately upon discovering that they are out of order.  During the first pass, the first and second elements are compared, say R1 and R2, and if they are out of order (i.e. R1>R2), then they are interchanged; this process is repeated for second and third elements (R2 and R3), then for third and fourth element (R3 and R4), and so on.  After first pass, the largest element will be in the last position.  This process of comparing consecutive elements continues until the whole array is sorted.
  • 11.  Note: The whole process requires at the most n-1 passes. After each pass through the array, a check can be made to determine whether any interchange was made during that pass. If no interchange occurred in a particular pass, then the array must be sorted and no further passes are required. TRACIN G
  • 12. BUBBLESORT (K, N) – Given an array K of size N, this procedure rearranges the elements of array in ascending order. PASS and LAST variables are used for the pass counter and position of the last unsorted element, respectively. The variable I is used to index the elements of array. The variable EXCHS is used to count the number of interchanges made on any pass. 1. [Initialize] LAST  N 2. [Create a loop for the passes] Repeat thru step 5 for PASS = 1, 2, …, N - 1 3. [Initialize counter for no of interchanges for this pass] EXCHS  0 4. [Perform pair-wise comparisons on unsorted consecutive elements] Repeat for I = 1, 2, … LAST-1 If K[I[ > K[I+1] Then K[I] ↔ K[I+1] (Interchange elements) EXCHS  EXCHS + 1 5. [Check if any interchange was made on this pass] If EXCHS = 0 Then Return (Array Sorted; Return early) Else LAST  LAST – 1 (Reduce size of unsorted list) 6. [Finished] Return
  • 13. INSERTION SORT  Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, or merge sort.  Every repetition of insertion sort removes an element from the input data, inserting it into the correct position in the already-sorted list, until no input elements remain.
  • 14.  Sorting is typically done in-place. The resulting array after k iterations has the property where the first k + 1 entries are sorted. In each iteration the first remaining entry of the input is removed, inserted into the result at the correct position, thus extending the result: BEFORE AFTER
  • 15.
  • 16. InsSort(A[1…N]) – For i = 2 to N { temp = A[i] j = i-1 while(j>0 and temp<A[j]) { A[j+1] = A[j] j = j-1 } A[j+1] = temp } ALGORITHM:
  • 17. Unsorted Array: 3, 7, 4, 9, 5, 2, 6, 1 i temp j 1 7 0 3 7 4 9 5 2 6 1 2 4 1 3 7 7 9 5 2 6 1 3 4 7 9 5 2 6 10 3 9 2 3 4 7 9 5 2 6 1 4 5 3 3 4 7 9 9 2 6 1 3 4 7 7 9 2 6 1 3 4 5 7 9 2 6 1 2 1 5 2 4 3 4 5 7 9 9 6 1 3 4 5 7 7 9 6 1 3 4 5 5 7 9 6 1 3 4 4 5 7 9 6 1 3 3 4 5 7 9 6 1 2 3 4 5 7 9 6 1 3 2 1 0 -1 6 6 5 2 3 4 5 7 9 9 1 2 3 4 5 7 7 9 1 2 3 4 5 6 7 9 1 4 3 7 1 6 2 3 4 5 6 7 9 9 2 3 4 5 6 7 7 9 2 3 4 5 6 6 7 9 2 3 4 5 5 6 7 9 2 3 4 4 5 6 7 9 2 3 3 4 5 6 7 9 2 2 3 4 5 6 7 9 5 4 3 2 1 0 -1