operations on arrays

32
Unit 5 Searching and Sorting Operations on arrays

Upload: others

Post on 23-Apr-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operations on arrays

Unit 5 Searching and Sorting Operations on arrays

Page 2: Operations on arrays

1. SEARCH • Searching means finding the location of the

element of an array along with its value. A given key can help find the location of the element. Search methods are two types: linear and binary.

1. Linear search:

• In the case of linear search, each element of the array is compared one by one with the given value of the element to be located. This method, which traverses the array sequentially to locate the given value of element is called linear search.

Page 3: Operations on arrays

//LINEAR SEARCH OF ELEMENT IN AN ARRAY

#include <stdio.h>

int main()

{

int series[40];

int count, loc, item,n,i;

printf("Enter the size of the list:");

scanf("%d",&n);

printf("\n Enter the list:");

for(i=0;i<n;i++)

scanf("%d", &series[i]);

printf("\n Enter the value to be searched\n");

scanf("%d", &item);

count=0;

loc=-1;

while(count<n) { if(series[count]==item) loc=count; count++; } if(loc>-1) printf("The position of the element w.r.t zero %d",loc); else printf("unsuccessful search"); return 0; }

Page 4: Operations on arrays

Drawbacks of linear search:

• In the linear search, the search starts from the first element and continues in a sequential manner from element to element till the desired element is found. If the desired element happens to be the last in the array, the entire search process will have to be repeated N times, where N is the size of the array being searched.

Page 5: Operations on arrays

Binary Search • The search operation can be the made faster by

the binary search method. This search works on the principle of divide and win. The array is sorted first in either the ascending or descending order. It is then divided into two halves. The desired element either in the first half of the array or in the second half of the array. Because the array is in order(ascending or descending), it will only be necessary to repeat this process on one of the two halves or segments, thereby reducing the remaining number of elements to be searched very rapidly.

Page 6: Operations on arrays

//BINARY SEARCH ELEMENT IN AN ARRAY

#include <stdio.h>

int main()

{

int series[50];

int i,n,loc, item;

int beg,end,mid;

printf("enter the size of the list:\n");

scanf("%d",&n);

printf("\n Enter the list:");

for(i=0;i<n;i++)

scanf("%d", &series[i]);

beg=0;

loc=-1;

end=n-1;

printf("\n Enter the value to be searched"); scanf("%d",&item); while((beg<=end)&&(loc==-1)) { mid=(beg+end)/2; if(series[mid]==item) loc=mid; else if(series[mid]<item) beg=mid+1; else end=mid-1; } if(loc>-1) printf("\n The position of the element is %d",++loc); else printf("\n Unsuccessful search"); return 0; }

Page 7: Operations on arrays

ILLUSTRATION OF BINARY SEARCH METHOD

• Consider the array of seven elements. the elements of an array are 12,14,16,19,23,27, and 31. assume that we want to search the element 23 from the list of elements. The steps are as follows:

Page 8: Operations on arrays

• 1. The element 12 and 31 are at positions first and last, respectively

• Calculate the mid-value which is as • MID=(FIRST+LAST)/2 • MID=(0+6)/2 • MID=3 • The key element 23 is to be compared with the mid-

value. If the key is less than the mid then the key element is present in the first half else in the other half; in this case, the key is on the right half. Hence, first is equal to middle+1

Arr[0] Arr[1] Arr[2] Arr[3] Arr[4] Arr[5] Arr[6]

12 14 16 19 23 27 31

Page 9: Operations on arrays

Arr[0] Arr[1] Arr[2] Arr[3] Arr[4] Arr[5] Arr[6]

12 14 16 19 23 27 31

First MIDDLE Last

Calculate the middle of the second half MIDDLE=(FIRST+LAST)/2 MIDDLE=(4+6)/2 MIDDLE=5 Again, the middle divides the second half into two parts. The key element 23 is lesser then the middle 27, hence it is present in the left half. LAST=MIDDLE-1 The middle is calculated as MIDDLE=(FIRST+LAST)/2 MIDDLE=(4+4)/2 MIDDLE=4

Page 10: Operations on arrays

• Now, the middle is 4 position and the middle element is 23. the key is searched successfully.

Arr[0] Arr[1] Arr[2] Arr[3] Arr[4] Arr[5] Arr[6]

12 14 16 19 23 27 31

Middle

Page 11: Operations on arrays

2. SORTING • Sorting is the process of arranging data in a

particular order(either ascending or descending). Arrays are convenient for storing many forms of data because they allow the access of any element via its index. There are several methods of sorting data held in one dimensional arrays. One of the sorting methods is the sequential method. Arrays can be sorted sequentially through the following ways: selection sort, bubble sort, insertion sort.

Page 12: Operations on arrays

i. Selection sort • This is the most common method of sorting data

stored in one dimensional array. This method searches for the smallest element and places it in the first location ARR[0]. Then it finds the second smallest element and puts it in ARR[1] and so on.

• In selection sort, two operations are performed repeatedly: selection and exchange. The algorithm for the selection of the smallest element and its position in the unsorted part of the array can be written in the body of a loop. However, the exchange with the first element of the unsorted part should be carried out outside the loop.

Page 13: Operations on arrays

Sort the following numbers manually using the selection sort method:

80,17,27,58,20,66,55

The contents of the list after each pass are shown below:

Pass

1. 17 80 27 58 20 66 55

2. 17 20 27 58 80 66 55

3. 17 20 27 58 80 66 55

4. 17 20 27 55 80 66 58

5. 17 20 27 55 58 66 80

6. 17 20 27 55 58 66 80

Page 14: Operations on arrays

Sort the following numbers manually using the selection sort method:

42,29,74,11,65,58

The contents of the list after each pass are shown below:

Pass

1. 11 29 74 42 65 58

2. 11 29 74 42 65 58

3. 11 29 42 74 65 58

4. 11 29 42 58 65 74

5. 11 29 42 58 65 74

Page 15: Operations on arrays

//SELECTION SORT

#include <stdio.h>

#include<conio.h>

int main()

{

int arr[30];

int min,pass;

int temp,i,j,n;

printf("Enter the size of the list:");

scanf("%d", &n);

printf("Enter the list:");

for(i=0;i<n;i++)

scanf("%d", &arr[i]);

for(i=0;i<n-1;i++) { min=arr[i]; pass=i; //find the smallest in the unsorted part for(j=i+1;j<n;j++) { if(min>arr[j]){ min=arr[j]; pass=j; } } temp=arr[i]; arr[i]=arr[pass]; arr[pass]=temp; } printf("The sorted list is:\n"); for(i=0;i<n;i++) { printf("%d ", arr[i]); } return 0; }

Page 16: Operations on arrays

Bubble sort:

• In the bubble sorting method of sorting, on

each pass through an array, each pair of

adjacent items is compared. If they are out of

order they are swapped before going on to the

next pair. The search operation moves from

left to right.

Page 17: Operations on arrays

1. 80 17 27 58 20 66 55

2. 17 80 27 58 20 66 55

3. 17 27 80 58 20 66 55

4. 17 27 58 80 20 66 55

5. 17 27 58 20 80 66 55

6. 17 27 58 20 66 80 55

7. 17 27 58 20 66 55 80

Sort the following numbers manually using the bubble sort method: 80,17,27,58,20,66,55 The contents of the list after each pass are shown below:

First pass

Page 18: Operations on arrays

1. 17 27 58 20 66 55 80

2. 17 27 58 20 66 55 80

3. 17 27 58 20 66 55 80

4. 17 27 20 58 66 55 80

5. 17 27 20 58 66 55 80

6. 17 27 20 58 55 66 80

Second pass

Page 19: Operations on arrays

1. 17 27 20 58 55 66 80 2. 17 27 20 58 55 66 80 3. 17 20 27 58 55 66 80 4. 17 20 27 58 55 66 80 5. 17 20 27 55 58 66 80

Third pass 1. 17 20 27 55 58 66 80 2. 17 20 27 55 58 66 80 3. 17 20 27 55 58 66 80 4. 17 20 27 55 58 66 80

Fourth pass

Page 20: Operations on arrays

1. 42 29 74 11 65 58

2. 29 42 74 11 65 58

3. 29 42 74 11 65 58

3. 29 42 11 74 65 58

4. 29 42 11 65 74 58

5. 29 42 11 65 58 74

Sort the following numbers manually using the bubble sort method: 42,29,74,11,65,58 The contents of the list after each pass are shown below:

First pass

Page 21: Operations on arrays

1. 29 42 11 65 58 74

2. 29 42 11 65 58 74

3. 29 11 42 65 58 74

4. 29 11 42 65 58 74

5. 29 11 42 58 65 74

Second pass

Page 22: Operations on arrays

1. 29 11 42 58 65 74 2. 11 29 42 58 65 74 3. 11 29 42 58 65 74 4. 11 29 42 58 65 74

Third pass

1. 11 29 42 58 65 74 2. 11 29 42 58 65 74 3. 11 29 42 58 65 74

Fourth pass

Page 23: Operations on arrays

//BUBBLE SORT

#include <stdio.h>

int main()

{

int arr[30];

int key,i,j,n;

printf("Enter the size of the list:");

scanf("%d", &n);

printf("Enter the list:");

for(i=0;i<n;i++)

scanf("%d", &arr[i]);

i=0; while(i<n-1) { i++; //swapping done for(j=0;j<n-i; j++) { if(arr[j]>arr[j+1]) { key=arr[j]; arr[j]=arr[j+1]; arr[j+1]=key; } } } printf("The sorted list is:\n"); for(i=0;i<n;i++) printf("%d ", arr[i]); getch(); }

Page 24: Operations on arrays

iii. Insertion sort

• This sorting method divides the list into two parts: the sorted part and the unsorted part. It picks up one element from the front of the unsorted part and inserts it at a suitable place in the sorted part. This process is repeated till the unsorted part is exhausted.

Page 25: Operations on arrays

Sort the following numbers manually using the insertion method: 80,17,27,58,20,66,55 • The contents of the list after each pass are shown below:

Pass

1. 17 80 27 58 20 66 55

2. 17 27 80 58 20 66 55

3. 17 27 58 80 20 66 55

4. 17 20 27 58 80 66 55

5. 17 20 27 58 66 80 55

6. 17 20 27 55 58 66 80

Page 26: Operations on arrays

Sort the following numbers manually using the insertion method: 42, 29,74,11,65,58 • The contents of the list after each pass are shown below:

Pass

1. 29 42 58 20 66 55

2. 17 27 80 58 20 66 55

3. 17 27 58 80 20 66 55

4. 17 20 27 58 80 66 55

5. 17 20 27 58 66 80 55

6. 17 20 27 55 58 66 80

Page 27: Operations on arrays

//INSERTION SORT

#include<stdio.h>

int main()

{

int arr[30];

int i,j,n,key;

printf("Enter the size of the list:");

scanf("%d", &n);

printf("Enter the list:");

for(i=0;i<n;i++)

scanf("%d", &arr[i]);

for(i=1; i<n;i++) { key=arr[i]; j=i-1; while((arr[j]>key)&&(j>=0)) { arr[j+1]=arr[j]; j=j-1; } arr[j+1]=key; } printf("\n The sorted list is--\n"); for(i=0;i<n;i++) printf("%d ", arr[i]); return 0; }

Page 28: Operations on arrays

iv. Merge sort:

• Merging is the technique for combining several sorted arrays into a single sorted array.

Page 29: Operations on arrays
Page 30: Operations on arrays
Page 31: Operations on arrays
Page 32: Operations on arrays

#include<stdio.h>

int main()

{

int arr1[30], arr2[30], flist[40];

int m,n,ptr1,ptr2, ptr3;

int i;

printf("Enter the size of lists 1 and 2\n");

scanf("%d %d", &m, &n);

printf("Enter list1:");

for(i=0;i<m;i++)

scanf("%d",&arr1[i]);

printf("Enter list2:");

for(i=0;i<n;i++)

scanf("%d",&arr2[i]);

ptr1=ptr2=ptr3=0; while(ptr1<m && ptr2<n) { if(arr1[ptr1]<arr2[ptr2]) flist[ptr3++]=arr1[ptr1++]; else flist[ptr3++]=arr2[ptr2++]; } while(ptr1<m) flist[ptr3++]=arr1[ptr1++]; while(ptr2<n) flist[ptr3++]=arr2[ptr2++]; printf("The final list is --\n"); for(i=0;i<m+n;i++) printf("%d ", flist[i]); return 0; }