Lab 1 BubbleSort

1
Lab 01 : Linear search and Buble Sort Problem : 01 Linear search #include <stdio.h> int main() { int list[10], key, num, i; printf("Enter no.of elements : "); scanf("%d", &num); printf("Enter %d elements\n", num); for (i = 1; i<=num; i++) scanf("%d", &list[i]); printf("\n enter the value to be searched: "); scanf("%d", &key); i = 1; while (key != list[i] && i<num) { i++; } if (key == list[i]) printf("\n %d element is found at location %d", list[i], i); Problem : 02 Buble Sort #include <stdio.h> int main() { int a[10],i,j,temp,n; printf("\n enter the max no.of elements u wanna sort: "); scanf("%d",&n); printf("\n enter the elements u want to sort: "); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } for(i=1;i<=n-1;i++) for(j=1;j<=n-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } printf("\nSort Elements are: "); for(i=1;i<=n;i++) { printf("%d\t",a[i]);

description

Bubble Sort in C

Transcript of Lab 1 BubbleSort

Page 1: Lab 1 BubbleSort

Lab 01 : Linear search and Buble Sort

Problem : 01 Linear search

#include <stdio.h>

int main(){

int list[10], key, num, i;

printf("Enter no.of elements : ");scanf("%d", &num);printf("Enter %d elements\n", num);for (i = 1; i<=num; i++)

scanf("%d", &list[i]);printf("\n enter the value to be searched:

");scanf("%d", &key);i = 1;while (key != list[i] && i<num){

i++;}if (key == list[i])

printf("\n %d element is found at location %d", list[i], i);

elseprintf("search is unsucessful");

return 0;}

Problem : 02 Buble Sort

#include <stdio.h>

int main(){int a[10],i,j,temp,n;

printf("\n enter the max no.of elements u wanna sort: ");scanf("%d",&n);printf("\n enter the elements u want to sort: ");for(i=1;i<=n;i++){scanf("%d",&a[i]);}for(i=1;i<=n-1;i++)for(j=1;j<=n-i;j++){if(a[j]>a[j+1]){temp=a[j];a[j]=a[j+1];a[j+1]=temp;}}printf("\nSort Elements are: ");for(i=1;i<=n;i++){printf("%d\t",a[i]);}

return 0;}