c programming array & shorting

Post on 13-Jul-2015

181 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

NOTES

NOTES

NOTES

NOTES

#include<stdio.h>

#include<conio.h>

void main()

{

int arr[10], i;

clrscr();

printf(“Enter Array Element\n”);

for(i=0;i<10;i++)

{

scanf(“%d”, &arr[i]);

}

printf(“Array Element\n”);

for(i=0;i<10;i++)

{

printf(“%d\n”,arr[i]);

}

getch();

}

EXTRA

EXTRA

EXTRA

NOTES

EXTRA

EXTRA

#include<stdio.h>#include<conio.h>void main(){

int arr[100], s, i , j, temp; clrscr();printf(“Enter Array Size\n”);scanf(“%d”,&s);printf(“Enter Array Element\n”);for(i=0;i<s;i++){

scanf(“%d”, &arr[i]);}printf(“Array Element befor sorting\n”);for(i=0;i<s;i++){

printf(“%d\n”, arr[i]);}

EXTRA

for(i=0;i<s-1;i++){

for(j=0;j<s-1-i;j++){

if(arr[j]>arr[j+1]){

temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;

}}

}printf(“Data after sorting \n”);for(i=0;i<s;i++){

printf(“%d\n”, arr[i]);}getch();}

EXTRA

EXTRA

#include<stdio.h>#include<conio.h>void main(){

int arr[100], s, i , j, temp; clrscr();printf(“Enter Array Size\n”);sxcanf(“%d”,&s);printf(“Enter Array Element\n”);for(i=0;i<s;i++){

scanf(“%d”, &arr[i]);}printf(“Array Element befor sorting\n”);for(i=0;i<s;i++){

printf(“%d\n”, arr[i]);}

EXTRA

for(i=0;i<s-1;i++){

for(j=i+1;j<s;j++){

if(arr[i]>arr[j]){

temp=arr[i];arr[i]=arr[j];arr[j]=temp;

}}

}printf(“Data after sorting\n”);for(i=0;i<s;i++){

printf(“%d\n”,arr[i]);}getch();}

EXTRA

EXTRA

EXTRA

#include<stdio.h>#include<conio.h>void main(){

int arr[10],s, p, temp,i,k;clrscr();printf("enter Array Size\n");scanf("%d",&s);printf("Enter Array Element\n");for(i=0;i<s;i++){

scanf("%d",&arr[i]);}printf("Array Element\n");for(i=0;i<s;i++){

printf("%d\n",arr[i]);}

EXTRA

for(i=1;i<s;i++){

p=i-1;temp=arr[i];while(temp<arr[p]&&p>=0){

arr[p+1]=arr[p];p--;

}arr[p+1]=temp;

}printf("Data after sorting\n");for(i=0;i<s;i++){

printf("%d\n",arr[i]);}getch();

}

EXTRA

Searching

Linear Search Binary Search

Linear Search- This is the simplest technique to find

out an element in an unsorted list. In this technique the value of the key is compared with the first element of the list, if match is found then the search is terminated. Otherwise next element from the list is fetched and compared with the key and this process is continued till the key is found or list is completely.

EXTRA

Binary Search- Binary search works for sorted lists and is avery efficient searching technique. It is used to find thelocation of a given element or record in a list. Otherinformation associated with the element can also befetched if required. To find the location of a file in thecomputer directory one can use this searching technique.Let low represents lower limit of the list l, high upper andmid is average of low and high that is middle of the lowerand upper limits.Mid = (low+high)/2;we compare the middle element with key to be searched. Ifthe value of middle element is greater than key ,then keywill exit in lower half of the list, take high = mid-1 and findnew value of mid.

EXTRA

#include<stdio.h>#include<conio.h>void main(){

int arr[100],s, i, first=0, mid=0, last =0, count =0, num;clrscr();printf(“Enter Array Size\n”);scanf(“%d”,&s);printf(“Enter Array Element\n”);for(i=0;i<s;i++){

scanf(“%d”,&arr[i]);}printf(“Array Element\n”);for(i=0;i<s;i++){

printf(“%d\n”,arr[i]);}

EXTRA

printf(“Enter the no want to search \n”);scanf(“%d”,&num);last = s-1;while((first<=last)&&(count==0)){

mid=(first+last)/2;if(arr[i]==num){

count=mid;}else if(arr[i]<num){

first=mid+1;}else {

last=mid-1;}

}

EXTRA

if(count>0){

printf(“No found”);}else{

printf(“No not found”);}getch();}

EXTRA

Double Dimension Array#include<stdio.h>

#include<conio.h>

void main()

{

int arr[3][3], i, j;

clrscr();

printf(“Enter Array Element\n”);

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

scanf(“%d”,&arr[i][j]);

}

}

printf(“Matrix \n\n”);

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf(“%d ”,arr[i][j]);

}

printf(“\n”);

}

getch();

}

EXTRA

top related