SlideShare a Scribd company logo
www.cscareerblog.com
www.cscareerblog.com
Searching and Sorting
Searching is an operation or a technique that helps finds the place of a given element
or value in the list. Any search is said to be successful or unsuccessful depending upon
whether the element that is being searched is found or not.
Sorting is refers to the operation or technique of arranging and rearranging sets of data
in some specific order. A collection of records called a list where every record is having one
or more fields. The fields which contains unique value for each record, is termed as the key
field.
The records are either sorted either numerically or alphanumerically. The records are then
arranged in ascending or descending order depending on the numerical value of the key
Linear search
Linear search is also called as sequential search. Linear search is a method for finding a
particular value in a list. In this searching technique you need to check every elements one by one
until desired element found.
This search process starts comparing of search element with the first element in the list. If
both are matching then results with element found otherwise search element is compared with next
element in the list. If both are matched, then the result is "element found". Otherwise, repeat the same
with the next element in the list until search element is compared with last element in the list, if that
last element also doesn't match, then the result is "Element not found in the list". That means, the
search element is compared with element by element in the list.
Search element 12
Step 1
Search element (12) is compared with first element (65)
both are not matching. So move to next element
Step 2
Search element (12) is compared with first element (20)
both are not matching. So move to next element
Step 3
Search element (12) is compared with first element (10)
both are not matching. So move to next element
Step 4
Search element (12) is compared with first element (55)
www.cscareerblog.com
www.cscareerblog.com
both are not matching. So move to next element
Step 5
Search element (12) is compared with first element (32)
both are not matching. So move to next element
Step 6
Search element (12) is compared with first element (12)
Both are matching. So we stop comparing and display element found at index 5.
Example
#include<iostream>
using namespace std;
int main()
{
int arr[10], i, num, n, c=0, pos;
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
www.cscareerblog.com
www.cscareerblog.com
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
return 0;
}
Output:
Binary search
The binary search can not be used for list of element which are in random order. This search
process starts comparing of the search element with the middle element in the list. If both are
matched, then the result is "element found". Otherwise, we check whether the search element is
smaller or larger than the middle element in the list. If the search element is smaller, then we repeat
the same process for left sublist of the middle element. If the search element is larger, then we repeat
the same process for right sublist of the middle element. We repeat this process until we find the
search element in the list or until we left with a sublist of only one element. And if that element also
doesn't match with the search element, then the result is "Element not found in the list".
Search element 12
Step 1
Search element (12) is compared with middle element (50)
Both are not matching. And 12 is smaller than 50. So we search only in the left sublist (i.e. 10,12,20
& 32).
Step 2
Search element (12) is compared with middle element (12)
Both are matching. So the result is “element found at index 1”
Search element 80
www.cscareerblog.com
www.cscareerblog.com
Step 1
Search element (80) is compared with middle element (50)
Both are not matching. And 80 is larger than 50.so we search only in the right sublist (i.e. 55,65,80 &
99 ).
Step 2
Search element (80) is compared with middle element (65)
Both are not matching. And 80 is larger than 65.so we search only in the right sublist (i.e. 80 & 99 ).
Step 3
Search element (80) is compared with middle element (80)
Both are not matching. So the result is “ element found at index 7”
Example
#include<iostream>
using namespace std;
int main()
{
int n, i, arr[50], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
www.cscareerblog.com
www.cscareerblog.com
if(arr[middle] < search)
{
first = middle + 1;
}
else if(arr[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
return 0;
}
Output:
Selection sort
Sorting is refers to the operation or technique of arranging and rearranging sets of data
in some specific order. A collection of records called a list where every record is having one
or more fields. The fields which contains unique value for each record, is termed as the key
field.
The records are either sorted either numerically or alphanumerically. The records are then
arranged in ascending or descending order depending on the numerical value of the key
Unsorted list
1st iteration:
Smallest = 5
2 < 5, smallest = 2
1 < 2, smallest = 1
4 > 1, smallest = 1
www.cscareerblog.com
www.cscareerblog.com
3 > 1, smallest = 1
Swap 5 and 1
2nd iteration:
Smallest = 2
2 < 5, smallest = 2
2 < 4, smallest = 2
2 < 3, smallest = 2
No Swap
3rd iteration:
Smallest = 5
4 < 5, smallest = 4
3 < 4, smallest = 3
Swap 5 and 3
4th iteration:
Smallest = 4
4 < 5, smallest = 4
No Swap
Finally,
Example
#include<iostream>
using namespace std;
int main()
{
int size, arr[50], i, j, temp, index, small, count=0;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
cin>>arr[i];
cout<<"Sorting array using selection sort...n";
for(i=0; i<(size-1); i++)
{
small = arr[i];
for(j=(i+1); j<size; j++)
{
if(small>arr[j])
www.cscareerblog.com
www.cscareerblog.com
{
small = arr[j];
count++;
index = j;
}
}
if(count!=0)
{
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
count=0;
}
cout<<"Now the Array after sorting is :n";
for(i=0; i<size; i++)
cout<<arr[i]<<" ";
return 0;
}
Output:
Bubble sort
The bubble sort makes multiple passes through a list. It compares adjacent items and
exchanges those that are out of order. Each pass through the list places the next largest value in its
proper place. In essence, each item “bubbles” up to the location where it belongs.
Figure shows the first pass of a bubble sort. The shaded items are being compared to see if
they are out of order. If there are n items in the list, then there are n−1n−1 pairs of items that need to
be compared on the first pass. It is important to note that once the largest value in the list is part of a
pair, it will continually be moved along until the pass is complete.
www.cscareerblog.com
www.cscareerblog.com
At the start of the second pass, the largest value is now in place. There are n−1n−1 items left
to sort, meaning that there will be n−2n−2 pairs. Since each pass places the next largest value in place,
the total number of passes necessary will be n−1n−1. After completing the n−1n−1 passes, the
smallest item must be in the correct position with no further processing required. ActiveCode 1 shows
the completebubbleSort function. It takes the list as a parameter, and modifies it by exchanging items
as necessary.
The exchange operation, sometimes called a “swap,” is slightly different in Python than in
most other programming languages. Typically, swapping two elements in a list requires a temporary
storage location (an additional memory location). A code fragment such as
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
will exchange the ith and jth items in the list. Without the temporary storage, one of the values would
be overwritten.
Example:
#include<iostream>
using namespace std;
int main()
{
int n, i, arr[50], j, temp;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
www.cscareerblog.com
www.cscareerblog.com
{
cin>>arr[i];
}
cout<<"Sorting array using bubble sort technique...n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Elements sorted successfully..!!n";
cout<<"Sorted list in ascending order :n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
Output:
Insertion sort
Insertion sort algorithm arranges a list of elements in a particular order. In insertion sort
algorithm, every iteration moves an element from unsorted portion to sorted portion until all the
elements are sorted in the list.
Consider the following list of elements..
Iteration 1:
Select the first position element in the list compare it with all other elements in the list and whenever
we found a smaller element that the element at first position then swap those two elements.
www.cscareerblog.com
www.cscareerblog.com
List after 1st
iteration
Iteration 2:
Select the second position element in the list compare it with all other elements in the list and
whenever we found a smaller element then the element at first position then swap those two elements.
List after 2nd
iteration
Iteration 3:
Select the third position element in the list compare it with all other elements in the list and whenever
we found a smaller element then the element at first position then swap those two elements.
List after 3rd
iteration
Iteration 4:
Select the fourth position element in the list compare it with all other elements in the list and
whenever we found a smaller element then the element at first position then swap those two elements.
List after 4th
iteration
Iteration 5:
www.cscareerblog.com
www.cscareerblog.com
Select the fifth position element in the list compare it with all other elements in the list and whenever
we found a smaller element then the element at first position then swap those two elements.
List after 5th
iteration
Iteration 6:
Select the sixth position element in the list compare it with all other elements in the list and whenever
we found a smaller element then the element at first position then swap those two elements.
List after 6th
iteration
Iteration 7:
Select the seventh position element in the list compare it with all other elements in the list and
whenever we found a smaller element then the element at first position then swap those two elements.
List after 7th
iteration
This is final sort
Example:
#include<iostream>
using namespace std;
int main()
{
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort ... n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
www.cscareerblog.com
www.cscareerblog.com
cout<<"Array after sorting : n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
Output:

More Related Content

PPTX
F sharp lists & dictionary
DrRajeshreeKhande
 
DOCX
C++ detyrat postim_slideshare
tctal
 
PDF
Collections Framework Begineers guide 2
Kenji HASUNUMA
 
PPTX
Groovy
Vijay Shukla
 
PDF
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
PDF
Lucene
Matt Wood
 
PDF
Python list
Mohammed Sikander
 
PDF
Python Workshop Part 2. LUG Maniapl
Ankur Shrivastava
 
F sharp lists & dictionary
DrRajeshreeKhande
 
C++ detyrat postim_slideshare
tctal
 
Collections Framework Begineers guide 2
Kenji HASUNUMA
 
Groovy
Vijay Shukla
 
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Lucene
Matt Wood
 
Python list
Mohammed Sikander
 
Python Workshop Part 2. LUG Maniapl
Ankur Shrivastava
 

What's hot (19)

PDF
List , tuples, dictionaries and regular expressions in python
channa basava
 
PPTX
Java 8 revealed
Sazzadur Rahaman
 
PPTX
Unit 4 python -list methods
narmadhakin
 
PDF
Data type list_methods_in_python
deepalishinkar1
 
PPTX
List in Python
Siddique Ibrahim
 
ZIP
レガシーコード改善ガイド
Maki Toshio
 
PPT
Sql
tech4us
 
PDF
An introduction to property-based testing
Vincent Pradeilles
 
PDF
Csharp_List
Micheal Ogundero
 
PPTX
Hashing and separate chain
VijayapriyaPandi
 
PDF
Collections Api - Java
Drishti Bhalla
 
PDF
Python List Comprehensions
Yos Riady
 
TXT
20150730 Sql AutoCommannotator - sac v2
Sharon Liu
 
PDF
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
DEEPANSHU GUPTA
 
PPTX
Python Programming Essentials - M12 - Lists
P3 InfoTech Solutions Pvt. Ltd.
 
PPT
Jhtp5 20 Datastructures
martha leon
 
PDF
Jquery 17-visual-cheat-sheet1
Michael Andersen
 
PPTX
Python list
ArchanaBhumkar
 
List , tuples, dictionaries and regular expressions in python
channa basava
 
Java 8 revealed
Sazzadur Rahaman
 
Unit 4 python -list methods
narmadhakin
 
Data type list_methods_in_python
deepalishinkar1
 
List in Python
Siddique Ibrahim
 
レガシーコード改善ガイド
Maki Toshio
 
Sql
tech4us
 
An introduction to property-based testing
Vincent Pradeilles
 
Csharp_List
Micheal Ogundero
 
Hashing and separate chain
VijayapriyaPandi
 
Collections Api - Java
Drishti Bhalla
 
Python List Comprehensions
Yos Riady
 
20150730 Sql AutoCommannotator - sac v2
Sharon Liu
 
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
DEEPANSHU GUPTA
 
Python Programming Essentials - M12 - Lists
P3 InfoTech Solutions Pvt. Ltd.
 
Jhtp5 20 Datastructures
martha leon
 
Jquery 17-visual-cheat-sheet1
Michael Andersen
 
Python list
ArchanaBhumkar
 
Ad

Similar to Searching and sorting by B kirron Reddi (20)

PDF
Searching
A. S. M. Shafi
 
PPT
Chap10
Terry Yoast
 
PPTX
Searching and Sorting Algorithms in Data Structures
poongothai11
 
PDF
Sorting
A. S. M. Shafi
 
PPT
Searching Sorting
guest2cb109
 
PPTX
data structures and algorithms Unit 3
infanciaj
 
DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PPTX
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
PDF
Sorting
Kariman Karm Gabaa
 
PPTX
Address calculation-sort
Vasim Pathan
 
PPT
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
PPTX
Lecture of algorithms and problem solving
menewratings009
 
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
PDF
advanced searching and sorting.pdf
haramaya university
 
PPTX
Data structure chapter 2 Time complexity of known algorithms.pptx
fikadumeuedu
 
PPT
search_sort.ppt
SwatiHans10
 
PPT
Chapter 14
Terry Yoast
 
PPT
1 D Arrays in C++
poonam.rwalia
 
Searching
A. S. M. Shafi
 
Chap10
Terry Yoast
 
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Searching Sorting
guest2cb109
 
data structures and algorithms Unit 3
infanciaj
 
MODULE 5-Searching and-sorting
nikshaikh786
 
searching in data structure.pptx
chouguleamruta24
 
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
Address calculation-sort
Vasim Pathan
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
Lecture of algorithms and problem solving
menewratings009
 
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
advanced searching and sorting.pdf
haramaya university
 
Data structure chapter 2 Time complexity of known algorithms.pptx
fikadumeuedu
 
search_sort.ppt
SwatiHans10
 
Chapter 14
Terry Yoast
 
1 D Arrays in C++
poonam.rwalia
 
Ad

More from B.Kirron Reddi (18)

PDF
What after graduation_-_mba
B.Kirron Reddi
 
PDF
What after graduation_-_banks
B.Kirron Reddi
 
PDF
What after graduation_-_mca
B.Kirron Reddi
 
PDF
Daa chpater14
B.Kirron Reddi
 
PDF
Daa chpater 12
B.Kirron Reddi
 
PDF
Daa chapter13
B.Kirron Reddi
 
PDF
Daa chapter11
B.Kirron Reddi
 
PDF
Daa chapter10
B.Kirron Reddi
 
PDF
Daa chapter9
B.Kirron Reddi
 
PDF
Daa chapter8
B.Kirron Reddi
 
PDF
Daa chapter7
B.Kirron Reddi
 
PDF
Daa chapter6
B.Kirron Reddi
 
PDF
Daa chapter5
B.Kirron Reddi
 
PDF
Daa chapter4
B.Kirron Reddi
 
PDF
Daa chapter 3
B.Kirron Reddi
 
PDF
Daa chapter 2
B.Kirron Reddi
 
PDF
Daa chapter 1
B.Kirron Reddi
 
PDF
Daa contents by B.Kirron Reddi
B.Kirron Reddi
 
What after graduation_-_mba
B.Kirron Reddi
 
What after graduation_-_banks
B.Kirron Reddi
 
What after graduation_-_mca
B.Kirron Reddi
 
Daa chpater14
B.Kirron Reddi
 
Daa chpater 12
B.Kirron Reddi
 
Daa chapter13
B.Kirron Reddi
 
Daa chapter11
B.Kirron Reddi
 
Daa chapter10
B.Kirron Reddi
 
Daa chapter9
B.Kirron Reddi
 
Daa chapter8
B.Kirron Reddi
 
Daa chapter7
B.Kirron Reddi
 
Daa chapter6
B.Kirron Reddi
 
Daa chapter5
B.Kirron Reddi
 
Daa chapter4
B.Kirron Reddi
 
Daa chapter 3
B.Kirron Reddi
 
Daa chapter 2
B.Kirron Reddi
 
Daa chapter 1
B.Kirron Reddi
 
Daa contents by B.Kirron Reddi
B.Kirron Reddi
 

Recently uploaded (20)

PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
CDH. pptx
AneetaSharma15
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
CDH. pptx
AneetaSharma15
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 

Searching and sorting by B kirron Reddi

  • 1. www.cscareerblog.com www.cscareerblog.com Searching and Sorting Searching is an operation or a technique that helps finds the place of a given element or value in the list. Any search is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. Sorting is refers to the operation or technique of arranging and rearranging sets of data in some specific order. A collection of records called a list where every record is having one or more fields. The fields which contains unique value for each record, is termed as the key field. The records are either sorted either numerically or alphanumerically. The records are then arranged in ascending or descending order depending on the numerical value of the key Linear search Linear search is also called as sequential search. Linear search is a method for finding a particular value in a list. In this searching technique you need to check every elements one by one until desired element found. This search process starts comparing of search element with the first element in the list. If both are matching then results with element found otherwise search element is compared with next element in the list. If both are matched, then the result is "element found". Otherwise, repeat the same with the next element in the list until search element is compared with last element in the list, if that last element also doesn't match, then the result is "Element not found in the list". That means, the search element is compared with element by element in the list. Search element 12 Step 1 Search element (12) is compared with first element (65) both are not matching. So move to next element Step 2 Search element (12) is compared with first element (20) both are not matching. So move to next element Step 3 Search element (12) is compared with first element (10) both are not matching. So move to next element Step 4 Search element (12) is compared with first element (55)
  • 2. www.cscareerblog.com www.cscareerblog.com both are not matching. So move to next element Step 5 Search element (12) is compared with first element (32) both are not matching. So move to next element Step 6 Search element (12) is compared with first element (12) Both are matching. So we stop comparing and display element found at index 5. Example #include<iostream> using namespace std; int main() { int arr[10], i, num, n, c=0, pos; cout<<"Enter the array size : "; cin>>n; cout<<"Enter Array Elements : "; for(i=0; i<n; i++) { cin>>arr[i]; } cout<<"Enter the number to be search : "; cin>>num; for(i=0; i<n; i++) { if(arr[i]==num) { c=1; pos=i+1; break; } } if(c==0)
  • 3. www.cscareerblog.com www.cscareerblog.com { cout<<"Number not found..!!"; } else { cout<<num<<" found at position "<<pos; } return 0; } Output: Binary search The binary search can not be used for list of element which are in random order. This search process starts comparing of the search element with the middle element in the list. If both are matched, then the result is "element found". Otherwise, we check whether the search element is smaller or larger than the middle element in the list. If the search element is smaller, then we repeat the same process for left sublist of the middle element. If the search element is larger, then we repeat the same process for right sublist of the middle element. We repeat this process until we find the search element in the list or until we left with a sublist of only one element. And if that element also doesn't match with the search element, then the result is "Element not found in the list". Search element 12 Step 1 Search element (12) is compared with middle element (50) Both are not matching. And 12 is smaller than 50. So we search only in the left sublist (i.e. 10,12,20 & 32). Step 2 Search element (12) is compared with middle element (12) Both are matching. So the result is “element found at index 1” Search element 80
  • 4. www.cscareerblog.com www.cscareerblog.com Step 1 Search element (80) is compared with middle element (50) Both are not matching. And 80 is larger than 50.so we search only in the right sublist (i.e. 55,65,80 & 99 ). Step 2 Search element (80) is compared with middle element (65) Both are not matching. And 80 is larger than 65.so we search only in the right sublist (i.e. 80 & 99 ). Step 3 Search element (80) is compared with middle element (80) Both are not matching. So the result is “ element found at index 7” Example #include<iostream> using namespace std; int main() { int n, i, arr[50], search, first, last, middle; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter "<<n<<" number :"; for (i=0; i<n; i++) { cin>>arr[i]; } cout<<"Enter a number to find :"; cin>>search; first = 0; last = n-1; middle = (first+last)/2; while (first <= last) {
  • 5. www.cscareerblog.com www.cscareerblog.com if(arr[middle] < search) { first = middle + 1; } else if(arr[middle] == search) { cout<<search<<" found at location "<<middle+1<<"n"; break; } else { last = middle - 1; } middle = (first + last)/2; } if(first > last) { cout<<"Not found! "<<search<<" is not present in the list."; } return 0; } Output: Selection sort Sorting is refers to the operation or technique of arranging and rearranging sets of data in some specific order. A collection of records called a list where every record is having one or more fields. The fields which contains unique value for each record, is termed as the key field. The records are either sorted either numerically or alphanumerically. The records are then arranged in ascending or descending order depending on the numerical value of the key Unsorted list 1st iteration: Smallest = 5 2 < 5, smallest = 2 1 < 2, smallest = 1 4 > 1, smallest = 1
  • 6. www.cscareerblog.com www.cscareerblog.com 3 > 1, smallest = 1 Swap 5 and 1 2nd iteration: Smallest = 2 2 < 5, smallest = 2 2 < 4, smallest = 2 2 < 3, smallest = 2 No Swap 3rd iteration: Smallest = 5 4 < 5, smallest = 4 3 < 4, smallest = 3 Swap 5 and 3 4th iteration: Smallest = 4 4 < 5, smallest = 4 No Swap Finally, Example #include<iostream> using namespace std; int main() { int size, arr[50], i, j, temp, index, small, count=0; cout<<"Enter Array Size : "; cin>>size; cout<<"Enter Array Elements : "; for(i=0; i<size; i++) cin>>arr[i]; cout<<"Sorting array using selection sort...n"; for(i=0; i<(size-1); i++) { small = arr[i]; for(j=(i+1); j<size; j++) { if(small>arr[j])
  • 7. www.cscareerblog.com www.cscareerblog.com { small = arr[j]; count++; index = j; } } if(count!=0) { temp = arr[i]; arr[i] = small; arr[index] = temp; } count=0; } cout<<"Now the Array after sorting is :n"; for(i=0; i<size; i++) cout<<arr[i]<<" "; return 0; } Output: Bubble sort The bubble sort makes multiple passes through a list. It compares adjacent items and exchanges those that are out of order. Each pass through the list places the next largest value in its proper place. In essence, each item “bubbles” up to the location where it belongs. Figure shows the first pass of a bubble sort. The shaded items are being compared to see if they are out of order. If there are n items in the list, then there are n−1n−1 pairs of items that need to be compared on the first pass. It is important to note that once the largest value in the list is part of a pair, it will continually be moved along until the pass is complete.
  • 8. www.cscareerblog.com www.cscareerblog.com At the start of the second pass, the largest value is now in place. There are n−1n−1 items left to sort, meaning that there will be n−2n−2 pairs. Since each pass places the next largest value in place, the total number of passes necessary will be n−1n−1. After completing the n−1n−1 passes, the smallest item must be in the correct position with no further processing required. ActiveCode 1 shows the completebubbleSort function. It takes the list as a parameter, and modifies it by exchanging items as necessary. The exchange operation, sometimes called a “swap,” is slightly different in Python than in most other programming languages. Typically, swapping two elements in a list requires a temporary storage location (an additional memory location). A code fragment such as temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; will exchange the ith and jth items in the list. Without the temporary storage, one of the values would be overwritten. Example: #include<iostream> using namespace std; int main() { int n, i, arr[50], j, temp; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter "<<n<<" numbers :"; for(i=0; i<n; i++)
  • 9. www.cscareerblog.com www.cscareerblog.com { cin>>arr[i]; } cout<<"Sorting array using bubble sort technique...n"; for(i=0; i<(n-1); i++) { for(j=0; j<(n-i-1); j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } cout<<"Elements sorted successfully..!!n"; cout<<"Sorted list in ascending order :n"; for(i=0; i<n; i++) { cout<<arr[i]<<" "; } return 0; } Output: Insertion sort Insertion sort algorithm arranges a list of elements in a particular order. In insertion sort algorithm, every iteration moves an element from unsorted portion to sorted portion until all the elements are sorted in the list. Consider the following list of elements.. Iteration 1: Select the first position element in the list compare it with all other elements in the list and whenever we found a smaller element that the element at first position then swap those two elements.
  • 10. www.cscareerblog.com www.cscareerblog.com List after 1st iteration Iteration 2: Select the second position element in the list compare it with all other elements in the list and whenever we found a smaller element then the element at first position then swap those two elements. List after 2nd iteration Iteration 3: Select the third position element in the list compare it with all other elements in the list and whenever we found a smaller element then the element at first position then swap those two elements. List after 3rd iteration Iteration 4: Select the fourth position element in the list compare it with all other elements in the list and whenever we found a smaller element then the element at first position then swap those two elements. List after 4th iteration Iteration 5:
  • 11. www.cscareerblog.com www.cscareerblog.com Select the fifth position element in the list compare it with all other elements in the list and whenever we found a smaller element then the element at first position then swap those two elements. List after 5th iteration Iteration 6: Select the sixth position element in the list compare it with all other elements in the list and whenever we found a smaller element then the element at first position then swap those two elements. List after 6th iteration Iteration 7: Select the seventh position element in the list compare it with all other elements in the list and whenever we found a smaller element then the element at first position then swap those two elements. List after 7th iteration This is final sort Example: #include<iostream> using namespace std; int main() { int size, arr[50], i, j, temp; cout<<"Enter Array Size : "; cin>>size; cout<<"Enter Array Elements : "; for(i=0; i<size; i++) { cin>>arr[i]; } cout<<"Sorting array using selection sort ... n"; for(i=1; i<size; i++) { temp=arr[i]; j=i-1; while((temp<arr[j]) && (j>=0)) { arr[j+1]=arr[j]; j=j-1; } arr[j+1]=temp; }
  • 12. www.cscareerblog.com www.cscareerblog.com cout<<"Array after sorting : n"; for(i=0; i<size; i++) { cout<<arr[i]<<" "; } return 0; } Output: