The document contains code for sorting an array using different sorting algorithms like bubble sort, selection sort, and insertion sort. It includes the algorithms, C code to implement each sorting technique on an integer array, and a menu driven program to choose between the different sorting options.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
275 views
Data Structures Lab-2
The document contains code for sorting an array using different sorting algorithms like bubble sort, selection sort, and insertion sort. It includes the algorithms, C code to implement each sorting technique on an integer array, and a menu driven program to choose between the different sorting options.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13
LAB-2
1. WAP to sort an array.
#include <stdio.h> void main () { int num[20]; int i, j, a, n; printf("enter number of elements in an array\n"); scanf("%d", &n); printf("Enter the elements\n"); for (i = 0; i < n; ++i) scanf("%d", &num[i]); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { if (num[i] > num[j]) { a = num[i]; num[i] = num[j]; num[j] = a; } } } printf("The Sorted Array:\n"); for (i = 0; i < n; ++i) { printf("%d\t", num[i]); } }
2. WAP to sort the array using Bubble Sorting.
ALGORITHM: - 1. For I = L TO U 2. { FOR J = L TO [ (U – 1) – I ] //need not consider already settled heavy elements// //that is why ( u -1 ) – I. 3. { if AR[J] > AR[J + 1] then { /* swap the values */ 4. temp = AR[J] 5. AR[J] = AR[J + 1] 6. AR[J + 1] = tmp } /* end of if */ } /* end of inner loop */ } /* end of outer loop */ 7. END. #include<conio.h> #include<stdio.h> void Bsort(int a[], int n) { int t; for(int i=0;i<(n-1);i++) { for(int j=(i+1);j<n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } } void main() { int a[10], i; printf("Enter the Elements\n"); for(i=0;i<10;i++) { scanf("%d", &a[i]); } Bsort(a,10); printf("Array After Bubble Sorting\n"); for(int q=0;q<10;q++) printf("%d\t",a[q]); }
3. WAP to sort the array using Selection Sorting.
ALGORITHM: - 1. small = AR[L] /* Initialise small with first array element */ 2. For I = L TO U do // In C, 0 To size-1 { 3. small = AR[ I ], pos = I /*Loop to find smallest element and its position */ 4. For J = I TO U do { If AR[J] < small then 5. { small = AR[J] 6. pos = J } J=J+1 } /* swap the smallest element with Ith element */ 8. temp = AR[ I ] 9. AR[ I ] = small 10. AR[pos] = temp } /* end of outer loop*/ 11. END. #include<conio.h> #include<stdio.h> void Ssort(int a[], int n) { int spos, t; for(int i=0;i<(n-1);i++) { spos=i; for(int j=(i+1);j<n;j++) { if(a[spos]>a[j]) { spos=j; } } t=a[spos]; a[spos]=a[i]; a[i]=t; } } void main() { int a[10], i; printf("Enter the Elements\n"); for(i=0;i<10;i++) { scanf("%d", &a[i]); } Ssort(a,10); printf("Array After Selection Sorting\n"); for(int q=0;q<10;q++) printf("%d\t",a[q]); } 4. WAP to sort the array using Insertion Sorting. ALGORITHM: - //To make A[0] the sentinel element by storing minimum possible integer value// 1. A[0] = minimum integer-value /* Now start sorting the array */ 2. Repeat steps 3 through 8 for K = 1, 2, 3 . . . N – 1 { 3. temp = A[K] 4. ptr = K – 1 5. Repeat steps 6 to 7 while temp < A[ptr] { 6. A[ptr + 1] = A[ptr] // Moves element forward 7. ptr = ptr – 1 } // End of inner repeat – step 5’s loop 8. A[ptr + 1] = temp } // End of outer repeat – step 2’s loop 9. END. #include<conio.h> #include<stdio.h> void Isort(int a[], int n) { int i, j, c, pos; for(i=1;i<n;i++) { c=a[i]; pos=0; while((pos<i)&&(a[pos]<=c)) pos++; if(pos!=i) { for(j=(i-1);j>=pos;j--) a[j+1]=a[j]; a[pos]=c; } } } void main() { int a[10], i; printf("Enter the Elements\n"); for(i=0;i<10;i++) { scanf("%d", &a[i]); } Isort(a,10); printf("Array After Insertion Sorting\n"); for(int q=0;q<10;q++) printf("%d\t",a[q]); } 5. Write a menu driven program with functions for bubble sort, insertion sort and selection sort #include<conio.h> #include<stdio.h> #include<stdlib.h> void Bsort(int a[], int n) { int t; for(int i=0;i<(n-1);i++) { for(int j=(i+1);j<n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } } void Ssort(int a[], int n) { int spos, t; for(int i=0;i<(n-1);i++) { spos=i; for(int j=(i+1);j<n;j++) { if(a[spos]>a[j]) { spos=j; } } t=a[spos]; a[spos]=a[i]; a[i]=t; } } void Isort(int a[], int n) { int i, j, c, pos; for(i=1;i<n;i++) { c=a[i]; pos=0; while((pos<i)&&(a[pos]<=c)) pos++; if(pos!=i) { for(j=(i-1);j>=pos;j--) a[j+1]=a[j]; a[pos]=c; } } } void main() { int a[10], i, ch; printf("Enter the Elements\n"); for(i=0;i<10;i++) { scanf("%d", &a[i]); } printf("1. Bubble Sorting\n"); printf("2. Selection Sorting\n"); printf("3. Insertion Sorting\n"); printf("4. Exit\n"); printf("Enter Your Choice\n"); scanf("%d", &ch); switch(ch) { case 1: Bsort(a, 10); break; case 2: Ssort(a, 10); break; case 3: Isort(a, 10); break; default: exit(0); } printf("Array After Sorting\n"); for(int q=0;q<10;q++) printf("%d\t",a[q]); }