SlideShare a Scribd company logo
Searching
1. Searching is the process to find the given
element in the list with its position.
2. Searching technique should be able to locate
the element to be searched as quickly as
possible.
3. The search is said to be successful if the given
element is found i.e. the element does exist in
the collection such as an array; other wise it is
unsuccessful.
Linear / Sequential Search
1. In Linear Search we start searching –
sequentially starting from the first element and
continue until we find the target [ element ]
or we are sure that it is not in the list.
2. This approach gives us 2 possibility –
i> either we find it
ii> as we reach the end of the list end of the list
Algorithm
1. Read the total number of array elements in ‘n’,
Set flag = o.
2. Read the ‘n’ array elements in array ‘a’ as – a [o]
… a [n].
3. Read the target to be searched in variable ‘Key’.
4. For i = o to n.
Compare each element of array with ‘Key’ =>
If (a [i] = = Key )
If true then set flag = 1,
and close the loop.
5. Now, if ( flag = = 1) means target found, so print
the location at which target is found.
6. Stop
Program:-
# include <stdio.h>
int main ()
{
int a[20],n,i, Key , pos , flag = o;
clrser ();
printf(“n Enter the number of elements
in array : n”);
scanf (“% d”, &n);
printf(“n Enter Array element = “);
For (i = o; i < n; i + +)
scanf(“%d”, &a[i]);
printf(“n Enter the number to search “);
scanf (“%d”, &Key);
For (i =o; i <n; i + +)
{
if (a[i] == Key)
{
pos = i‘;
flag = 1;
break ;
}
}
if (flag == 1 )
printf (“n Element located as %d “n”, pos);
else
[printf “n Element not present “);
getch ();
}
The complexity of any searching algorithm
depends on number of comparisons required to
find the element.
Performance of searching algorithm can be
found by counting the number of comparisons
in order to find the given element.
Advantages & disadvantages
1. Simple and easy method.
2. Efficient for only small lists.
3. Better for unsorted data.
4. Highly inefficient for large data.
5. Suitable for storage structure, which do not
support direct access to data.
eg – magnetic tape.
6. Best case = 1 comparison
Worst case = 1 n
Average case = (n+1)/2
7. Complexity = o (n)
Binary Search
1. The linear search algorithm is very slow. If we have
an array of 1000 elements, we must make 1000
comparisons in the worst case.
If the array is not sorted, the linear search is the
only solution.
However, if the array is sorted, we can use a more
efficient algorithm called the binary search.
So for binary search, the array should be sorted before
searching any target in it.
2. In this, the Key element is compared with the
middle element of the list.
If they are equal, the search is successful.
3. If the middle element is greater than the Key,
the search process is repeated with the lower
half of the list;
otherwise the process is repeated with
the upper half of the list.
4. Thus every time the comparison is made the
number of elements yet to be searched are
reduced by half.
5. The search is terminated when either the Key
element is found or determine that it is not in
the list.
6. The list is divided into two equal parts for
searching the method is called the Binary
search.
7. The efficiency of the searching method can be
improved by using the sorted list.
This will reduce the searching time.
#include <stdio.h>
int main()
{
int first, last, middle, array [20], i, n, search;
printf(“n Enter the number of elements in n”);
scanf (“% d”, &n);
printf(“n Enter %d numbers :n”, n );
For (i = o; i < n; i + +)
scanf(“%d”, &array [i]);
printf(“ Enter the value to find n” );
scanf (“%d”, &search );
for =o;
Last = n-1
Middle= (first + last ) 2;
While = (first < = last )
{
if (array [middle ] < search )
first = middle + 1 ;
else if (array [middle]==search )
{
printf(“%d found at %d n”, search, middle + 1);
break;
}
else
last = middle – 1;
middle = (first + last )/2;
}
if (first > last)
printf (“ Not found ! %d is not present
in the list n “, search);
getch ();
}
Advantages & Disadvantages
1. Suitable for sorted data.
2. Not suitable for unsorted data.
3. Efficient for large list.
Sorting
It refers to the operation of arranging
data in the some given order. i.e either
ascending order or descending order.
When elements are to be arranged in ascending
order, they are in increasing order
i.e arr[1] < arr[2] < arr[3] <………>arr[N]
And in descending order, they are in decreasing
order.
i.e arr[1] < arr[2] < arr[3] <………>arr[N]
eg – Suppose the list is as follows-
8,4,19,2,7,13,5,16
Ascending order = 2,4,5,7,8,13,16,19
Descending order = 19,16,13,8,7,5,4,2
Sorting is divided into 2 categories
1. Internal sort
2. External sort
1. In an internal sort, all the data are held in
primary memory during sorting process.
2. External sorting is carried on Secondary storage.
External sort uses primary memory for the data
currently being sorted and secondary storage for
any data that does not fit in primary memory.
For eg:- A file of 20,000 records may be sorted using
an array that holds only 1000 records.
During the sorting process, only 1000
records are therefore in memory of one time, the
other 19,000 are kept in a file in secondary storage.
Sorting Techniques
Bubble Sort
1. In this sorting method, to arrange elements
in ascending order.
2. We begin with 0th element is compared with
the 1st element. If it is found to be greater
than 1st element, then they are interchanged.
3. Then the 1st element is compared with the
2nd element, if it is found to be greater, then
they are interchanged.
4. In the same way all the elements [excluding last]
are compared with their next element and are
interchanged if required.
5. On completing the 1st iteration, the largest
element gets placed at the last position.
6. Similarly, in the second iteration, second largest
element gets placed at the second last position, and
so on.
7. As a result, after all the iterations, the list
become as a sorted list.
BUBBLE SORT ALGORITHM
1. Read the total number of elements /
numbers in a variable ‘n’.
2. Enter that many numbers and store them in
an array ‘a’.
3. Take for loop of ‘I’ as -
i = o to n
4. Take a for loop of ‘j’ in I’ s loop.
‘j’ = o to n- I
5. Compare a[j] and a[j+1]
6. If a[j] > a[j+1] then interchange
a[j] with a[j+1]
7. Close the for loop.
8. Print the array.
9. Exit.
PROGRAM
void main()
{
int a [ 20 ]
int n, i, j, temp
printf(“n Enter total no . of elements” );
scanf (“% d”, &n);
printf(“n Enter the array elements:” );
for (i = o; i < n; i + +)
scanf(“%d”, & a[i]);
for (i = o; i < n; i + +)
{
for (j = o; j<n-I; j++)
{
if (a[j] > a [j +1])
{
temp = a[ j ];
a [ j + 1] = temp;
}
}
}
printf(“nn Array after Sorting :n”);
for (I = o; i<n; i++);
printf(“%d”, a[i]);
getch ();
}
ADVANTAGES AND DISADVANTAGE
1. Simple and easy to implement.
2. Slow process.
3. Inefficient for large sorting list.
SELECTION SORT
1. Selection sort is more efficient than bubble
sort and insertion sort.
2. Suppose ‘x’ is an array of size ‘n’ stored in
memory.
3. The selection sort algorithm first selects the
smallest element in the array x and place it at
array position o.
4. Then it selects the next smallest element in
the array x and place it at array position 1.
5. It simply continues this procedure until it
places the biggest element in the last position of
the array.
SELECTION SORT ALGORITHM
1. Read the total number of array element in
variable ‘n’
2. Enter the array elements and store them in
array ’a’.
3. Take for loop for ‘I’ running from o to n-1.
4. Assign ‘I’ to minimum (variable ‘min’)
5. Take a for loop for ‘j’ running from i +1 to n.
6. Check if a[j]< a[in]
If true, then assign ‘j’ to min ; close j loop.
7. Now swap the two elements, so that we get
smallest element on its right position.
i.e temp = a [i]
a [i] = a [min]
a [min] = temp
close ‘I’ loop
8. Printf the sorted array.
9. stop.
PROGRAM
void main()
{
int a [ 20 ] i, j, n, temp
printf(“n Enter total no . of elements” );
scanf (“% d”, &n);
printf(“n Enter the array elements:” );
for (i = o; i < n; i + +)
scanf(“%d”, & a[i]);
for (i = o ; i < n-1 ; i + + )
{
min = i;
for ( j= I +1; j <n; j + +)
{
if (a [j] < a[min]
min = j ;
}
temp = a [i]
a [i] = a [min]
a [min] = temp
}
Printf(“n The sorted array is =“);
For (I = o; I <n ; I + +)
printf(“%d”, a [i]);
Getch ();
}
Complexity -
Worst
case
Average
case
Best case
0(n2) 0(n2) 0(n2)
INSERTION SORT
1. The insertion sort works just like its name
suggests – it inserts each item into its proper
place.
2. In the first iteration, second element A[1] is
compared with the first element A[o] in the
second iteration, third element is compared
with first and second element.
3. In general, in every iteration, an element is
compared with all the elements before it.
4. During comparison if it is found that the
element can be inserted at a suitable position,
then space is created for it by shifting the other
elements one position to the right and inserting
the element at the suitable position.
5. This procedure is repeated for all the
elements in the list
PROGRAM
#include <conio.h>
#include<stdio.h>
void main ()
{ int a[20], I, j, n, temp;
clrscr();
printf(“ Enter total no. of elements in array “);
scanf(“%d”, &n)
printf(“n The Array elements:”);
for (i = o; i<n; i + + )
{
temp = a [i];
j = i-1;
while ( ( temp < a [j] ) & (j>=o) )
{
a [j + 1 ] = a [j];
J = j-1;
}
a [j +1] = temp;
}
printf(“n Array after Sorting”);
for (i = o; i < n; i + +)
printf(“%d”, a[i]);
getch ();
}
MERGE SORT
1. Merge sort is a well-known example of divide
and conquer method.
2. Here, the elements are to be sorted in
ascending order.
3. Merging means combining two sorted lists
into one sorted list.
for this the element from both the sorted
lists are compared the smaller of both the
elements is then stored in the third array.
4. The sorting is complete when all the elements
from both the lists are placed in the third list.
RADIX SORT
1. Radix sort is also known as – digit, pocket and
bucket sorting.
2. Radix sort puts the elements in order by
comparing the digits of the numbers.
3. It is based on the values of the actual digits in
the positional representation.
4. For example- the number 235 in decimal
notation is written with – 2 in a hundreds
position.
- 3 in a ten’s position.
- 5 in the units position.
5. In this, each pass through the list orders the
data one digit at a time.
6. At the end of every pass, numbers in buckets
are merged to produce a common list.
7. Number of passes depends on the range of
numbers being sorted.
ALGORITHM FOR RADIX SORT
1. Declare the radix function above main() with
two parameter 1> array ‘a’
2> no. of array elements ‘n’
2. In the function main(),
declare the required variables as
a [15], n, i
3. Read the total number of elements in variable
’n’.
4. Read the ‘n’ elements in array a[i].
5. Call the radix function as -
radix ( a, n);
6. Print the sorted array.
7. Close function main().
8. Void radix ( int a[ ], int n)
{
Declare its required variables as –
Buckets [10] [10], count [10], passes, large,
div, bucket no, i, j, k.
9. Now, find the largest number in a[ ]-
large = a[o] // first element assigned to large
for (i = 1 to n)
if a[i] > large
assign a[i] to large.
10. Find the number of digits in large and store
the count in passes-
passes = o
while (large >o)
{
passes ++
large = large / 10
}
11. Perform radix sort as-
div = 1
for (i =1 to passes)
{ first initialize buckets –
for (j = o to 9)
count [j] = o
Insert elements in respective buckets –
for (j= o to n)
{
bucket no = (a [j] / div) % 10;
buckets [bucket no ] [count [bucket no ] ] = a[j]
count [bucket no ] ++
}
12. Merge elements of buckets and store them in a[o] …
a[n-1]
j=o
for (bucketno = o to 9)
for ( K=o to count [bucketno])
a[j ++] = buckets [bucket no] [K];
div = div * 10
• 13 . Close function radix.

More Related Content

Similar to PPT.pptx Searching and Sorting Techniques (20)

PDF
Chapter 14 Searching and Sorting
MuhammadBakri13
 
PPTX
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
PDF
Sorting
A. S. M. Shafi
 
PPT
Arrays
Aman Agarwal
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PDF
Chapter Two.pdf
abay golla
 
PPSX
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
SolomonEndalu
 
PDF
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
PDF
Searching and Sorting in Data Structures with examples
vimalaimogen5850
 
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
PPT
Sorting algorithms
CHANDAN KUMAR
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
PPT
C Language Unit-6
kasaragadda srinivasrao
 
PPT
Unit6 jwfiles
mrecedu
 
PPTX
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
PDF
Data structures arrays
maamir farooq
 
PPTX
data_structure_Chapter two_computer.pptx
Mohammed472103
 
Chapter 14 Searching and Sorting
MuhammadBakri13
 
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
Arrays
Aman Agarwal
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
Chapter Two.pdf
abay golla
 
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
SolomonEndalu
 
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
Searching and Sorting in Data Structures with examples
vimalaimogen5850
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Sorting algorithms
CHANDAN KUMAR
 
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
C Language Unit-6
kasaragadda srinivasrao
 
Unit6 jwfiles
mrecedu
 
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
Data structures arrays
maamir farooq
 
data_structure_Chapter two_computer.pptx
Mohammed472103
 

More from Vaibhav Parjane (13)

PPTX
Advantages and disadvantages of algorithm.pptx
Vaibhav Parjane
 
PPTX
Examples of Algorithms : Introduction to C.pptx
Vaibhav Parjane
 
PPTX
Topic 1.1 Fundamentals of Algorithm.pptx
Vaibhav Parjane
 
PPTX
Unit no 1(Fundamentals of Algorithm).pptx
Vaibhav Parjane
 
PPTX
Unit 1(1).pptx Program Logic Development
Vaibhav Parjane
 
PPTX
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
PPTX
Unit 2 Searching and Sorting Technique.pptx
Vaibhav Parjane
 
PPTX
unit 2 First.pptx Searching - Linear and Binary Search
Vaibhav Parjane
 
PPTX
unit 1 SIXTH.pptx Algorithm Complexity Time
Vaibhav Parjane
 
PPTX
Operations on Data Structure- Several Operation performed on DS
Vaibhav Parjane
 
PPTX
Classification of Data Structure -Linear and Non Linear
Vaibhav Parjane
 
PPTX
Need of Data Structure & Abstract Data Type
Vaibhav Parjane
 
PPTX
Introduction to data & Data Structure Definition
Vaibhav Parjane
 
Advantages and disadvantages of algorithm.pptx
Vaibhav Parjane
 
Examples of Algorithms : Introduction to C.pptx
Vaibhav Parjane
 
Topic 1.1 Fundamentals of Algorithm.pptx
Vaibhav Parjane
 
Unit no 1(Fundamentals of Algorithm).pptx
Vaibhav Parjane
 
Unit 1(1).pptx Program Logic Development
Vaibhav Parjane
 
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
Unit 2 Searching and Sorting Technique.pptx
Vaibhav Parjane
 
unit 2 First.pptx Searching - Linear and Binary Search
Vaibhav Parjane
 
unit 1 SIXTH.pptx Algorithm Complexity Time
Vaibhav Parjane
 
Operations on Data Structure- Several Operation performed on DS
Vaibhav Parjane
 
Classification of Data Structure -Linear and Non Linear
Vaibhav Parjane
 
Need of Data Structure & Abstract Data Type
Vaibhav Parjane
 
Introduction to data & Data Structure Definition
Vaibhav Parjane
 
Ad

Recently uploaded (20)

PDF
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PDF
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
PDF
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
PDF
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PPT
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
PPTX
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
Ad

PPT.pptx Searching and Sorting Techniques

  • 1. Searching 1. Searching is the process to find the given element in the list with its position. 2. Searching technique should be able to locate the element to be searched as quickly as possible. 3. The search is said to be successful if the given element is found i.e. the element does exist in the collection such as an array; other wise it is unsuccessful.
  • 2. Linear / Sequential Search 1. In Linear Search we start searching – sequentially starting from the first element and continue until we find the target [ element ] or we are sure that it is not in the list. 2. This approach gives us 2 possibility – i> either we find it ii> as we reach the end of the list end of the list
  • 3. Algorithm 1. Read the total number of array elements in ‘n’, Set flag = o. 2. Read the ‘n’ array elements in array ‘a’ as – a [o] … a [n]. 3. Read the target to be searched in variable ‘Key’. 4. For i = o to n. Compare each element of array with ‘Key’ => If (a [i] = = Key ) If true then set flag = 1, and close the loop.
  • 4. 5. Now, if ( flag = = 1) means target found, so print the location at which target is found. 6. Stop Program:- # include <stdio.h> int main () { int a[20],n,i, Key , pos , flag = o; clrser ();
  • 5. printf(“n Enter the number of elements in array : n”); scanf (“% d”, &n); printf(“n Enter Array element = “); For (i = o; i < n; i + +) scanf(“%d”, &a[i]); printf(“n Enter the number to search “); scanf (“%d”, &Key);
  • 6. For (i =o; i <n; i + +) { if (a[i] == Key) { pos = i‘; flag = 1; break ; } }
  • 7. if (flag == 1 ) printf (“n Element located as %d “n”, pos); else [printf “n Element not present “); getch (); } The complexity of any searching algorithm depends on number of comparisons required to find the element.
  • 8. Performance of searching algorithm can be found by counting the number of comparisons in order to find the given element.
  • 9. Advantages & disadvantages 1. Simple and easy method. 2. Efficient for only small lists. 3. Better for unsorted data. 4. Highly inefficient for large data. 5. Suitable for storage structure, which do not support direct access to data. eg – magnetic tape. 6. Best case = 1 comparison Worst case = 1 n Average case = (n+1)/2
  • 11. Binary Search 1. The linear search algorithm is very slow. If we have an array of 1000 elements, we must make 1000 comparisons in the worst case. If the array is not sorted, the linear search is the only solution. However, if the array is sorted, we can use a more efficient algorithm called the binary search. So for binary search, the array should be sorted before searching any target in it.
  • 12. 2. In this, the Key element is compared with the middle element of the list. If they are equal, the search is successful. 3. If the middle element is greater than the Key, the search process is repeated with the lower half of the list; otherwise the process is repeated with the upper half of the list.
  • 13. 4. Thus every time the comparison is made the number of elements yet to be searched are reduced by half. 5. The search is terminated when either the Key element is found or determine that it is not in the list. 6. The list is divided into two equal parts for searching the method is called the Binary search.
  • 14. 7. The efficiency of the searching method can be improved by using the sorted list. This will reduce the searching time.
  • 15. #include <stdio.h> int main() { int first, last, middle, array [20], i, n, search; printf(“n Enter the number of elements in n”); scanf (“% d”, &n); printf(“n Enter %d numbers :n”, n ); For (i = o; i < n; i + +) scanf(“%d”, &array [i]); printf(“ Enter the value to find n” ); scanf (“%d”, &search );
  • 16. for =o; Last = n-1 Middle= (first + last ) 2; While = (first < = last ) { if (array [middle ] < search ) first = middle + 1 ; else if (array [middle]==search ) {
  • 17. printf(“%d found at %d n”, search, middle + 1); break; } else last = middle – 1; middle = (first + last )/2; }
  • 18. if (first > last) printf (“ Not found ! %d is not present in the list n “, search); getch (); }
  • 19. Advantages & Disadvantages 1. Suitable for sorted data. 2. Not suitable for unsorted data. 3. Efficient for large list.
  • 20. Sorting It refers to the operation of arranging data in the some given order. i.e either ascending order or descending order. When elements are to be arranged in ascending order, they are in increasing order i.e arr[1] < arr[2] < arr[3] <………>arr[N] And in descending order, they are in decreasing order. i.e arr[1] < arr[2] < arr[3] <………>arr[N]
  • 21. eg – Suppose the list is as follows- 8,4,19,2,7,13,5,16 Ascending order = 2,4,5,7,8,13,16,19 Descending order = 19,16,13,8,7,5,4,2 Sorting is divided into 2 categories 1. Internal sort 2. External sort
  • 22. 1. In an internal sort, all the data are held in primary memory during sorting process. 2. External sorting is carried on Secondary storage. External sort uses primary memory for the data currently being sorted and secondary storage for any data that does not fit in primary memory. For eg:- A file of 20,000 records may be sorted using an array that holds only 1000 records. During the sorting process, only 1000 records are therefore in memory of one time, the other 19,000 are kept in a file in secondary storage.
  • 23. Sorting Techniques Bubble Sort 1. In this sorting method, to arrange elements in ascending order. 2. We begin with 0th element is compared with the 1st element. If it is found to be greater than 1st element, then they are interchanged. 3. Then the 1st element is compared with the 2nd element, if it is found to be greater, then they are interchanged.
  • 24. 4. In the same way all the elements [excluding last] are compared with their next element and are interchanged if required. 5. On completing the 1st iteration, the largest element gets placed at the last position. 6. Similarly, in the second iteration, second largest element gets placed at the second last position, and so on. 7. As a result, after all the iterations, the list become as a sorted list.
  • 25. BUBBLE SORT ALGORITHM 1. Read the total number of elements / numbers in a variable ‘n’. 2. Enter that many numbers and store them in an array ‘a’. 3. Take for loop of ‘I’ as - i = o to n 4. Take a for loop of ‘j’ in I’ s loop. ‘j’ = o to n- I
  • 26. 5. Compare a[j] and a[j+1] 6. If a[j] > a[j+1] then interchange a[j] with a[j+1] 7. Close the for loop. 8. Print the array. 9. Exit.
  • 27. PROGRAM void main() { int a [ 20 ] int n, i, j, temp printf(“n Enter total no . of elements” ); scanf (“% d”, &n); printf(“n Enter the array elements:” ); for (i = o; i < n; i + +) scanf(“%d”, & a[i]); for (i = o; i < n; i + +)
  • 28. { for (j = o; j<n-I; j++) { if (a[j] > a [j +1]) { temp = a[ j ]; a [ j + 1] = temp; } } } printf(“nn Array after Sorting :n”); for (I = o; i<n; i++); printf(“%d”, a[i]); getch (); }
  • 29. ADVANTAGES AND DISADVANTAGE 1. Simple and easy to implement. 2. Slow process. 3. Inefficient for large sorting list.
  • 30. SELECTION SORT 1. Selection sort is more efficient than bubble sort and insertion sort. 2. Suppose ‘x’ is an array of size ‘n’ stored in memory. 3. The selection sort algorithm first selects the smallest element in the array x and place it at array position o. 4. Then it selects the next smallest element in the array x and place it at array position 1.
  • 31. 5. It simply continues this procedure until it places the biggest element in the last position of the array.
  • 32. SELECTION SORT ALGORITHM 1. Read the total number of array element in variable ‘n’ 2. Enter the array elements and store them in array ’a’. 3. Take for loop for ‘I’ running from o to n-1. 4. Assign ‘I’ to minimum (variable ‘min’) 5. Take a for loop for ‘j’ running from i +1 to n.
  • 33. 6. Check if a[j]< a[in] If true, then assign ‘j’ to min ; close j loop. 7. Now swap the two elements, so that we get smallest element on its right position. i.e temp = a [i] a [i] = a [min] a [min] = temp close ‘I’ loop
  • 34. 8. Printf the sorted array. 9. stop.
  • 35. PROGRAM void main() { int a [ 20 ] i, j, n, temp printf(“n Enter total no . of elements” ); scanf (“% d”, &n); printf(“n Enter the array elements:” ); for (i = o; i < n; i + +) scanf(“%d”, & a[i]); for (i = o ; i < n-1 ; i + + )
  • 36. { min = i; for ( j= I +1; j <n; j + +) { if (a [j] < a[min] min = j ; } temp = a [i] a [i] = a [min] a [min] = temp }
  • 37. Printf(“n The sorted array is =“); For (I = o; I <n ; I + +) printf(“%d”, a [i]); Getch (); }
  • 39. INSERTION SORT 1. The insertion sort works just like its name suggests – it inserts each item into its proper place. 2. In the first iteration, second element A[1] is compared with the first element A[o] in the second iteration, third element is compared with first and second element. 3. In general, in every iteration, an element is compared with all the elements before it.
  • 40. 4. During comparison if it is found that the element can be inserted at a suitable position, then space is created for it by shifting the other elements one position to the right and inserting the element at the suitable position. 5. This procedure is repeated for all the elements in the list
  • 41. PROGRAM #include <conio.h> #include<stdio.h> void main () { int a[20], I, j, n, temp; clrscr(); printf(“ Enter total no. of elements in array “); scanf(“%d”, &n)
  • 42. printf(“n The Array elements:”); for (i = o; i<n; i + + ) { temp = a [i]; j = i-1; while ( ( temp < a [j] ) & (j>=o) ) { a [j + 1 ] = a [j];
  • 43. J = j-1; } a [j +1] = temp; } printf(“n Array after Sorting”); for (i = o; i < n; i + +) printf(“%d”, a[i]); getch (); }
  • 44. MERGE SORT 1. Merge sort is a well-known example of divide and conquer method. 2. Here, the elements are to be sorted in ascending order. 3. Merging means combining two sorted lists into one sorted list. for this the element from both the sorted lists are compared the smaller of both the elements is then stored in the third array.
  • 45. 4. The sorting is complete when all the elements from both the lists are placed in the third list.
  • 46. RADIX SORT 1. Radix sort is also known as – digit, pocket and bucket sorting. 2. Radix sort puts the elements in order by comparing the digits of the numbers. 3. It is based on the values of the actual digits in the positional representation. 4. For example- the number 235 in decimal notation is written with – 2 in a hundreds position.
  • 47. - 3 in a ten’s position. - 5 in the units position. 5. In this, each pass through the list orders the data one digit at a time. 6. At the end of every pass, numbers in buckets are merged to produce a common list. 7. Number of passes depends on the range of numbers being sorted.
  • 48. ALGORITHM FOR RADIX SORT 1. Declare the radix function above main() with two parameter 1> array ‘a’ 2> no. of array elements ‘n’ 2. In the function main(), declare the required variables as a [15], n, i 3. Read the total number of elements in variable ’n’.
  • 49. 4. Read the ‘n’ elements in array a[i]. 5. Call the radix function as - radix ( a, n); 6. Print the sorted array. 7. Close function main(). 8. Void radix ( int a[ ], int n) {
  • 50. Declare its required variables as – Buckets [10] [10], count [10], passes, large, div, bucket no, i, j, k. 9. Now, find the largest number in a[ ]- large = a[o] // first element assigned to large for (i = 1 to n) if a[i] > large assign a[i] to large.
  • 51. 10. Find the number of digits in large and store the count in passes- passes = o while (large >o) { passes ++ large = large / 10 }
  • 52. 11. Perform radix sort as- div = 1 for (i =1 to passes) { first initialize buckets – for (j = o to 9) count [j] = o Insert elements in respective buckets – for (j= o to n) {
  • 53. bucket no = (a [j] / div) % 10; buckets [bucket no ] [count [bucket no ] ] = a[j] count [bucket no ] ++ }
  • 54. 12. Merge elements of buckets and store them in a[o] … a[n-1] j=o for (bucketno = o to 9) for ( K=o to count [bucketno]) a[j ++] = buckets [bucket no] [K]; div = div * 10
  • 55. • 13 . Close function radix.