0% found this document useful (0 votes)
23 views6 pages

Unit3 Sortsearch

Uploaded by

dbdbdvhdhdbdj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Unit3 Sortsearch

Uploaded by

dbdbdvhdhdbdj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Computer Science and Engineering

M.V.S.R.Engineering College, Nadergul, Hyderabad.


B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : III
Teacher:- K.Srinivas

SORTING:- Sorting of an array is the technique of arranging the array elements in a specified order i.e either
ascending or descending order. For example, consider an array a with data 33,57,27,85,23. If they are arranged in
ascending order then sorted array is 23,27,33,57,85. There are several sorting algorithms available, for example bubble
sort, selection sort, shell sort, quick sort etc.

SELECTION SORT:- In Selection sort first we assume the first one is the smallest and compare with all other
elements to find the smallest one then we interchange the position of the values. So in one iteration the smallest will
be in right position. Leave the first element because it is in right position, repeat this process to sort all the elements of
array.

Suppose the numbers are stored in an array A are 5, 7, 2, 3, 1, 6

5 7 2 3 1 6

A0 A1 A2 A3 A4 A5

Consider the selection sort method on the array A in each iteration observe how sorting done.

Iteration 1:

(a) Compare A0 with A1,A2,A3,A4,A5 to find smallest value location it is true inter change location value of A0 with
the smallest value location i.e A4. as shown below
1, 7, 2, 3, 5, 6
At the end of the first iteration, the smallest number 1 has moved to the firs position of the array. However, the rest of the numbers
are not sorted.

Iteration 2:

(b) Compare A1 with A2, A3, A4, A5 to find smallest value location it is true inter change location value of A1 with the
smallest value location i.e A2. as shown below
1, 2,7,3,5, 6
Repeat this process until all array elements to be sorted. After all iterations the array as follows

1, 2, 3, 5, 6, 7

//selection sort Program ascending


#include<stdio.h>
void main()
{
int t,a[20],j,i,n,m;
printf("Enter the no of elements to enter\n");
scanf("%d",&n) ;
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{ m=i;
for(j=i+1;j<n;j++)
{ if(a[j]<a[m])
m=j;
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : III
Teacher:- K.Srinivas

}
t=a[i];
a[i]=a[m];
a[m]=t;
}
printf("Ordered elements are \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

OUTPUT: Enter the no of elements to enter


6
Enter the array elements
572316
Ordered elements are
1 2 3 5 6 7

BUBBLE SORT:- In bubble sort, each element is compared with its adjacent element. If the first element is larger
than the second one then the positions of the elements are interchanged, otherwise don’t change the elements. In the
first pass the largest element occupies its position (i.e. last position).In the next pass do the same but leave the last
element because the element occupies its original position. During the second pass the second largest element
occupies second last position. Continue this process until all the array elements are sorted.

Suppose the numbers are stored in an array A are 32,51,27,85,66,23,13,57

32 51 27 85 66 23 13 57

A1 A2 A3 A4 A5 A6 A7 A8

We apply bubble sort to the array A. We discuss each pass separately.

PASS 1:

(a) Compare A1 and A2; Since 32>51, the list is not altered.(it is falls)
32, 51, 27, 85, 66, 23, 13, 57
(b) Compare A2 and A3; since 51>27, interchange 51 and 27 as follows (it is true)
32, 27, 51, 85, 66, 23, 13, 57
(c) Compare A3 and A4, since 51>85, the list is not altered. .(it is falls)
32, 27, 51, 85, 66, 23, 13, 57
(d) Compare A4 and A5, since 85>66, interchange 85 and 66 as follows(it is true)
32, 27, 51, 66, 85, 23, 13, 57
(e) Compare A5 and A6, since 85>23, interchange 85 and 23 as follows(it is true)
32, 27, 51, 66, 23,85,13,57
(f) Compare A6 and A7, since 85>13, interchange 85 and 13 as follows(it is true)
32, 27, 51, 66, 23,13,85,57
(g) Compare A7 and A8, since 85>57, interchange 85 and 57 as follows(it is true)
32, 27, 51, 66, 23, 13, 57, 85

At the end of the first pass, the largest number, 85, has moved to the last position. However, the rest of the numbers are not sorted,
even though some of them have changed their positions.
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : III
Teacher:- K.Srinivas

Repeat the process for the remaining passes.

The following is the interchange in each pass

Pass 2: 27, 32, 51, 66, 23, 13, 57, 85

27, 32, 51, 23, 66, 13, 57, 85

27, 32, 51, 23, 13, 66, 57, 85

27, 32, 51, 23, 13, 57, 66, 85

At the end of the second pass, the second largest number, 66, has moved its way to next-to-last position.

Pass 3: 27, 32, 23, 51, 13, 57, 66, 85

27, 32, 23, 13, 51, 57, 66, 85

Pass 4: 27, 23, 33, 13, 51, 57, 66, 85

27, 23, 13, 32, 51, 57, 66, 85

Pass 5: 23,27, 13, 32, 51, 57,66, 85

23, 13, 27 32 51, 57 66, 85

Pass 6: 13, 23, 27, 32, 51, 57, 66, 85

Pass 6 actually have two comparisons, A1 with A2 and A2 and A3. The second comparison does not involve an interchange.

Pass 7: Finally, A1 is compared with A2. Since 13>23, no interchange takes place

//buble sort programs


#include<stdio.h>
void main()
{ int a[100],n,i,j,temp;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements into array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-1;i++)
{ for(j=0;j<n-1-i;j++)
{ if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Element of array after sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

OUTPUT: Enter the size of the array


8
Enter elements into array
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.

B.E(1st Year, EEE-II) 2019-2020, semester -I


Subject : Problem Sloving and Programming Unit : III
Teacher:- K.Srinivas

32 51 27 85 66 23 13 57

The elements in array after sorting


13 23 27 33 51 57 66 85

SEARCHING

Let DATA be a collection of data elements in memory, and suppose a specific ITEM of information is given.
Searching refers to the operation of finding the Location of ITEM in DATA or printing whether the element is Found
or not found. The search is said to successful if ITEM does appear in DATA and unsuccessful otherwise.

There are two type of searching techniques. They are

1) Linear searching
2) Binary Searching
LINEAR SEARCHING :- The linear search or sequential searching is most simple searching method. It does not
expect the list to be sorted also. The key, which is to be searched, is compared with each element of the list one by
one. If a match occurs, the search is successful and search has to terminate. Otherwise end of the list is reached, it
means that the search has failed and the key has no matching element in the list.

Example: Program to search an element in an array using linear search technique.

#include<stdio.h>
void main()
{
int a[10],i,n,flag=0,se;
printf(“Enter the size of the array\n”);
scanf(“%d”,&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the element to be searched\n");
scanf("%d",&se);
for(i=0;i<n;i++)
{ if(se==a[i])
{ flag=1;
printf("The %d is found in the array at %d pos\n",n,i+1);
break;
}
}
if(flag==0)
printf("There is no such element found in the array");
}

OUTPUT: Enter the size of the array


10
Enter the elements
23 45 36 45 78 53 64 81 29 30
Enter the element to be searched
53
The 53 is found in the array at 6th position
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : III
Teacher:- K.Srinivas
BINARY SEARCH:- The binary search is an extremely efficient algorithm. This search technique searches the given
item in minimum possible comparisons. To do the binary search, first the array elements in order then only it
work. The logic behind this technique is given below:

First find the middle element of the array. To find middle location of the array divide array size by 2.

The search key is compared with the middle element of the list. Now three situations may occur:

1) The middle element matches with the search key –the search ends successfully here.
2) If the Key(search element) lesser than middle element – then the value which we are searching is (possibly) in the
first half of the list(because elements are sorted). So search only the first half of the array. But here we have to set
the upper boundary to the array because we are making array as two parts. Set upper boundary as middle minus
one.
3) The Key(search element) greater than middle element – then the value which we are searching is(possibly) in the
second half of the list. So search only the second half of the array. But here we have to set the lower boundary to
the array because we are making array as two parts. Set lower boundary as middle plus one.

Repeat the same steps until an element is found in the search area. In this algorithm every time we are reducing the
search area. So no of comparisons keeps on decreasing. So it is efficient algorithm when compared to linear search but
the array has to be sorted before doing binary search.

Let the elements in the array A[12 ] in sorted order are

11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88

Now we apply binary search algorithm to search the element 40 in the list.

Specifically, LOW, HIGH and MID well have the following successive values

1) Initially, LOW=0 and HIGH=12. Hence


MID=(LOW+HIGH)/2=0+12/2=6 and so A[6]=55

2) Since 40<55, HIGH has its value changed by HIGH=MID-1=5. Hence

MID=(0+5)/2=2 and is A[2]=30

2) Since 40>30, LOW has its value changed by LOW=MID+1 Hence

MID=(3+6)/2=4 and so a[4]=40

We have found 40 in LOC 5 in the array.

LOW MID HIGH

11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88

LOW MID HIGH

11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88
LOW MID HIGH

11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88

Then MID==KEY then found key and stop search.

Otherwise repeat the above process until LOW <= HIGH.


Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : III
Teacher:- K.Srinivas

//Binary search program S


#include<stdio.h>
void main()
{ int a[50],low=0,high,mid,i,sz,key,flag=0;
printf("Enter the size of the array\n");
scanf("%d",&sz);

printf("Enter the elements into array in sorted order\n");


for(i=0;i<sz;i++)
scanf("%d",&a[i]);
printf("Enter the element to be searched\n");
scanf("%d",&key);
low=0;
high=sz-1;
while(low<=high)
{ mid=(low+high)/2;
if(a[mid]==key)
{ flag=1;
printf("The element %d found at %d th position\n",key,mid+1);
break;
}
else if(a[mid]>key)
high=mid-1;
else if(a[mid]<key)
low=mid+1;
}
if(flag==0)
printf("The element %d not found in the array\n",key);
}

OUTPUT:-
Enter the size of the array
12
Enter the elements into array in sorted order
11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88
Enter the element to be searched
40
The element 40 found at 5 th position.

You might also like