Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 5 Sorting and Searching: Bubble sort Selection sort

Similar presentations


Presentation on theme: "Module 5 Sorting and Searching: Bubble sort Selection sort"— Presentation transcript:

1 Module 5 Sorting and Searching: Bubble sort Selection sort
linear search Binary search. Scope rules Storage classes. Bit-wise operations

2 Sorting Sorting refers to ordering data in an increasing or decreasing fashion according to some linear relationship among the data items. Sorting can be done on names, numbers and records Different sortings are: Bubble sort Selection sort Heap Sort Radix Sort. Quick Sort. Merge Sort. Insertion sort.

3 Bubble sort Bubble sort algorithm starts by
comparing the first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, you mustn't swap the element. Then, again second and third elements are compared and swapped if it is necessary This process go on until last and second last element is compared and swapped. This completes the first step of bubble sort. If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to get required result

4

5 Sort Elements using Bubble Sort Algorithm
for(steps=0;step<n-1;step++) for(i=0;i<n-step-1;i++) { if(data[i]>data[i+1]) temp=data[i]; data[i]=data[i+1]; data[i+1]=temp; }

6 Selection sort Selection sort algorithm starts by
compairing first two elements of an array and swapping if necessary, if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is. Then, again first element and third element are compared and swapped if necessary. This process goes on until first and last element of an array is compared. This completes the first step of selection sort. If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to get required result.

7 for better performance,
in second step, comparison starts from second element because after first step, the required number is automatically placed at the first (i.e, In case of sorting in ascending order, smallest element will be at first and in case of sorting in descending order, largest element will be at first.). Similarly, in third step, comparison starts from third element and so on.

8

9 for(steps=0;steps<n;++steps) for(i=steps+1;i<n;++i) { if(data[steps]>data[i]) temp=data[steps]; data[steps]=data[i]; data[i]=temp; }

10 Linear Search Linear search is a very simple search algorithm.
In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.

11 Algorithm Linear Search ( Array A, Value x) Step 1: Set i to 1
Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit

12 printf("Enter the number to linear search for: ");
scanf("%d",&numsearch); for(i=0;i <= num-1;i++) { if(a[i] == numsearch) { boolnum = 1; break; } if(boolnum == 0) printf("The number is not in the list.\n"); else printf("The number is found.\n"); return 0;

13 Binary search Binary search is a fast search algorithm
This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in a sorted form.

14 printf("Enter value to find\n"); scanf("%d", &search); first = 0; 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 location %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); return 0;

15 C - Storage Classes A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. They precede the type that they modify.

16 Storage class of variable Determines following things
Where the variable is stored Scope of Variable Default initial value Lifetime of variable

17 four different storage classes
auto register static extern

18 The auto Storage Class The auto storage class is the default storage class for all local variables. { int mount; auto int month; } defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.

19 C auto storage class This is default storage class
All variables declared are of type Auto by default  In order to Explicit declaration of variable use ‘auto’ keyword auto int num1 ; // Explicit Declaration

20 Features

21 void main() { auto num = 20 ; { auto num = 60 ; printf("nNum : %d",num); } printf("nNum : %d",num); }

22 The register Storage Class
The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size { register int miles; } only be used for variables that require quick access such as counters.

23 Example #include<stdio.h> int main() { int num1,num2; register int sum; printf("\nEnter the Number 1 : "); scanf("%d",&num1); printf("\nEnter the Number 2 : "); scanf("%d",&num2); sum = num1 + num2; printf("\nSum of Numbers : %d",sum); return(0); }

24

25 Summary

26 The static Storage Class
A static variable tells the compiler to persist the variable until the end of program. Instead of creating and destroying a variable every time when it comes into and goes out of scope, static is initialized only once and remains into existence till the end of program. A static variable can either be internal or external depending upon the place of declaration. Scope of internal static variable remains inside the function in which it is defined. External static variables remain restricted to scope of file in each they are declared. They are assigned 0 (zero) as default value by the compiler

27 void test(); main() { test(); test(); } void test() { static int a = 0; //Static variable a = a+1; printf("%d\t",a); output : 1 2 3

28 The extern Storage Class
Variables of this storage class are “Global variables” Global Variables are declared outside the function and are accessible to all functions in the program Generally , External variables are declared again in the function using keyword extern In order to Explicit declaration of variable use ‘extern’ keyword

29 Features

30 Example int num = 75 ; void display(); void main() { extern int num ; printf("nNum : %d",num); display(); } void display() { extern int num ; printf("nNum : %d",num); }

31 Output Num : 75 Num : 75

32 Note : Declaration within the function indicates that the function uses external variable Functions belonging to same source code , does not require declaration (no need to write extern) If variable is defined outside the source code , then declaration using extern keyword is required

33 First File: main.c #include <stdio.h> int count ; extern void write_extern(); main() { count = 5; write_extern(); }

34 Second File: support.c #include <stdio.h> extern int count; void write_extern(void) { printf("count is %d\n", count); }

35 Example 3


Download ppt "Module 5 Sorting and Searching: Bubble sort Selection sort"

Similar presentations


Ads by Google