This program uses Bubble sort and Selection sort as well as Linear and Binary se
ID: 3551444 • Letter: T
Question
This program uses Bubble sort and Selection sort as well as Linear and Binary search.
Explanation / Answer
Bubble sort and Selection sort Linear and Binary search
#include<stdio.h> #define size 25 void selection(); int findmin(int []); int i,j,Arr[size],min,counter; int main() { printf(" Enter your elements now "); for(i=0;i<size;i++) scanf("%d",&Arr[i]); printf("You have provided this data "); for(i=0;i<size;i++) printf(" %d",Arr[i]); printf(" Press any key to perform selection sort "); selection(); printf("Result after selection sort "); for(i=0;i<size;i++) printf(" %d",Arr[i]); printf("Selection sort on this array took %d swap operation",counter); } void selection() { for(i=0;i<(size-1);i++) { j = findmin(Arr); if(Arr[i]>Arr[j]) { Arr[i] = Arr[i] + Arr[j]; Arr[j] = Arr[i] - Arr[j]; Arr[i] = Arr[i] - Arr[j]; counter++; } } } int findmin(int a[]) { min = i; for(j=i+1;j<size;j++) { if(a[j]<a[min]) min = j; } return(min); }Linear and Binary search#include <stdio.h> int main() { int c, first, last, middle, n, search, array[5000]; printf("Enter number of elements "); scanf("%d",&n); printf("Enter %d integers ", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to find "); 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. ", 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. ", search); return 0; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.