0% found this document useful (0 votes)
37 views

Lab Manual

signals processing

Uploaded by

Engr. Abdullah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Lab Manual

signals processing

Uploaded by

Engr. Abdullah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Manual /Assignment (7)

Sorting Algorithms

Student Name:--------------------------------Date: 02-05-2011 Roll No:------------------------------------------Grades: ---------------------

Instructions: All tasks should be completed in the coming Lab Session to get full credit. You must show the output and code of each task to the instructor. Attach separate A4 sheets with manual to complete the assignments if needed. Copying codes and assignments will only lead to embarrassment. So avoid it. Reading: [1] Data Structures and Algorithm Analysis in C by Mark Allen Weiss [2] Data Structures using C and C++ by Langsam, Augenstein, Tenebaum

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 it is not changed. Then next element is compared with its adjacent element and the same process is repeated for all the elements in the array until we get a sorted array. Let A be a linear array of n numbers. Sorting of A means rearranging
1/10

the elements of A so that they are in order. Here we are dealing with ascending order. i.e., A[1] < A[2] < A[3] < ...... A[n]. Suppose the list of numbers A[1], A[2], A[ n] is an element of array A. The bubble sort algorithm works as follows: Step 1: Compare A[1] and A[2] and arrange them in the (or desired) ascending order, so that A[1] < A[2].that is if A[1] is greater than A[2] then interchange the position of data by swap = A[1]; A[1] = A[2]; A[2] = swap. Then compare A[2] and A[3] and arrange them so that A[2] < A[3]. Continue the process until we compare A[N 1] with A[N]. Note: Step1 contains n 1 comparisons i.e., the largest element is bubbled up to the nth position or sinks to the nth position. When step 1 is completed A[N] will contain the largest element. Step 2: Repeat step 1 with one less comparisons that is, now stop comparison at A [n 1] and possibly rearrange A[N 2] and A[N 1] and so on. Note: in the first pass, step 2 involves n2 comparisons and the second largest element will occupy A[ n-1]. And in the second pass, step 2 involves n 3 comparisons and the 3rd largest element will occupy A[n 2] and so on. Step n 1: compare A[1]with A[2] and arrange them so that A[1] < A[2]. After n 1 steps, the array will be a sorted array in increasing (or ascending) order. The following figures will depict the various steps (or PASS) involved in the sorting of an array of 5 elements. The elements of an array A to be sorted are: 42, 33, 23, 74, 44
FIRST PASS 33 swapped 33 42 23 swapped 23 42 74 74 44 44 SECOND PASS 33 33 23 23 42 no swapping 42 74 44 swapped 44 74

23 swapped 23 33 33 no swapping 42 42

23 33 42 no swapping
2/10

44 74

44 74

44 74

THIRD PASS

23 no swapping 33 42 44 74
FOURTH PASS

23 33 no swapping 42 44 74

23 no swapping 33 42 44 74 Thus the sorted array is 23, 33, 42, 44, 74.

ALGORITHM
Let A be a linear array of n numbers. Swap is a temporary variable for swapping (or interchange) the position of the numbers. 1. Input n numbers of an array A 2. Initialise i = 0 and repeat through step 4 if (i < n) 3. Initialize j = 0 and repeat through step 4 if (j < n i 1) 4. If (A[j] > A[j + 1]) (a) Swap = A[j] (b) A[j] = A[j + 1] (c) A[j + 1] = Swap 5. Display the sorted numbers of array A 6. Exit. C Implementation #include<conio.h> #include<stdio.h> #define MAX 20 void main() { int arr[MAX],i,j,k,temp,n,xchanges;
3/10

printf (\nEnter the number of elements : ); scanf (%d,&n); for (i = 0; i < n; i++) { printf (E\nnter element %d : ,i+1); scanf (%d,&arr[i]); } printf (\nUnsorted list is :\n); for (i = 0; i < n; i++) printf (%d , arr[i]); printf (\n); /* Bubble sort*/ for (i = 0; i < n1 ; i++) { xchanges=0; for (j = 0; j <n1i; j++) { if (arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; xchanges++; }/*End of if*/ }/*End of inner for loop*/ if (xchanges==0) /*If list is sorted*/ break; printf(\nAfter Pass %d elements are : ,i+1); for (k = 0; k < n; k++) printf(%d , arr[k]); printf(\n);
4/10

}/*End of outer for loop*/ printf(\nSorted list is :\n); for (i = 0; i < n; i++) printf(%d , arr[i]); getch(); }/*End of main()*/

Selection Sort
Selection sort algorithm finds the smallest element of the array and interchanges it with the element in the first position of the array. Then it finds the second smallest element from the remaining elements in the array and places it in the second position of the array and so on. Let A be a linear array of n numbers, A [1], A [2], A [3],...... A [n]. Step 1: Find the smallest element in the array of n numbers A[1], A[2], ...... A[n]. Let LOC is the location of the smallest number in the array. Then interchange A[LOC] and A[1] by swap = A[LOC]; A[LOC] = A[1]; A[1] = Swap. Step 2: Find the second smallest number in the sub list of n 1 elements A [2] A [3] ...... A [n 1] (first element is already sorted). Now we concentrate on the rest of the elements in the array. Again A [LOC] is the smallest element in the remaining array and LOC the corresponding location then interchange A [LOC] and A [2].Now A [1] and A [2] is sorted, since A [1] less than or equal to A [2]. Step 3: Repeat the process by reducing one element each from the array Step n 1: Find the n 1 smallest number in the sub array of 2 elements (i.e., A(n 1), A (n)). Consider A [LOC] is the smallest element and LOC is its corresponding position. Then interchange A [LOC] and A(n 1). Now the array A [1], A [2], A [3], A [4],..A [n] will be a sorted array.

ALGORITHM
5/10

Let A be a linear array of n numbers A [1], A [2], A [3], A [k], A [k+1], .. A [n]. Swap be a temporary variable for swapping (or interchanging) the position of the numbers. Min is the variable to store smallest number and Loc is the location of the smallest element. 1. Input n numbers of an array A 2. Initialize i = 0 and repeat through step5 if (i < n 1) (a) min = a[i] (b) loc = i 3. Initialize j = i + 1 and repeat through step 4 if (j < n 1) 4. if (a[j] < min) (a) min = a[j] (b) loc = j 5. if (loc ! = i) (a) swap = a[i] (b) a[i] = a[loc] (c) a[loc] = swap 6. display the sorted numbers of array A 7. Exit

C Implementation
#include<conio.h> #include<stdio.h> #define MAX 20 void main() { int arr[MAX], i,j,k,n,temp,smallest; printf (\nEnter the number of elements : ); scanf (%d, & n); for (i = 0; i < n; i++) { printf (\nEnter element %d : ,i+1); scanf (%d, &arr[i]); } printf (\nUnsorted list is : \n); for (i = 0; i < n; i++) printf (%d , arr[i]); printf (\n);
6/10

/*Selection sort*/ for (i = 0; i < n 1 ; i++) { /*Find the smallest element*/ smallest = i; for(k = i + 1; k < n ; k++) { if (arr[smallest] > arr[k]) smallest = k ; } if ( i != smallest ) { temp = arr [i]; arr[i] = arr[smallest]; arr[smallest] = temp ; } printf (\nAfter Pass %d elements are : ,i+1); for (j = 0; j < n; j++) printf (%d , arr[ j]); printf (\n); }/*End of for*/ printf (\nSorted list is : \n); for (i = 0; i < n; i++) printf (%d , arr[i]); getch(); }/*End of main()*/

7/10

You might also like