Sorting & Searching Concepts
Sorting & Searching Concepts
1
Sorting a List of numbers
2
Sorting of a List
• Sorting data either in ascending or descending order is the important
computing application that can be achieved by various ways.
Example
5
Other Method:
Selection Sort
6
Next example
Selection Sort- C
C implementation: https://youtu.be/088_oErA6Rw
8
Searching in a List
9
#define SIZE 10 Program
int Search(int A[], int size, int key);
int main()
{ int data[ SIZE ], i, temp;
int searchKey; //value to search in the array
printf(Enter data items:\n");
for ( i = 0; i < SIZE; ++i)
scanf("%4d", data[ i ]); //search function code
int Search(int A[], int size, int key)
printf("Enter integer to search for: ");
{ int j;
scanf("%d", &searchKey);
for (j = 0; j < size; ++j)
{
//call Search function
if ( a[ j ] == query)
temp = Search(data, searchKey, SIZE);
{
if ( temp != -1) //condition for displaying result
return j; //return location of value
printf("Found value in element %d.", temp);
}
else
}
printf("Value not found.");
return -1; //value not found
return 0;
}
}
10
To do Task
1. Write program of bubble, selection and insertion methods
of sorting the list of given numbers.
11