Posted by
snehil
Saturday, January 29, 2011
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int a[100],n,i,j,temp,loc,min;
- clrscr();
- printf("\How many elements:\n");
- scanf("%d",&n);
- printf("Enter the element of array\n");
- for(i=0;i<=n-1;i++)
- {
- scanf("%d",&a[i]);
- }
- min=a[0];
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<=n-1;i++)
- {
- min=a[i];
- loc=i;
- for(j=i+1;j<=n-1;j++)
- {
- if(a[j]<min)
- {
- min=a[j];
- loc=j;
- }
- }
- if(loc!=1)
- {
- temp=a[i];
- a[i]=a[loc];
- a[loc]=temp;
- }
-
- }
- printf("The number after selection sorting are:\n");
- for(i=0;i<=n-1;i++)
- {
- printf("%d\n",a[i]);
- }
- getch();
- }
Posted by
snehil
Friday, January 28, 2011
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- #include<conio.h>
- #define max 100
- int a[max],n,i,l,h;
- void main()
- {
- void input(void);
- input();
- getch();
- }
-
- void input(void)
- {
- void output(int a[],int n);
- void quick_sort(int a[],int l,int h);
- printf("How many elements in the array : ");
- scanf("%d",&n);
- printf("\n");
- printf("Enter the elemennts : \n");
- for(i=0;i<=n-1;i++)
- {
- scanf("%d",&a[i]);
- }
- l=0;
- h=n-1;
- quick_sort(a,l,h);
- printf("Sorted Array :\n ");
- output(a,n);
- }
-
- void quick_sort(int a[],int l, int h)
- {
- int temp,key,low,high;
- low=l;
- high=h;
- key=a[(low+high)/2];
- do
- {
- while(key>a[low])
- {
- low++;
- }
- while(key<a[high])
- {
- high--;
- }
- if(low<=high)
- {
- temp=a[low];
- a[low++]=a[high];
- a[high--]=temp;
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- } while(low<=high);
- if(l<high)
- quick_sort(a,l,high);
- if(low<h)
- quick_sort(a,low,h);
- }
- void output(int a[],int n)
- {
- for(i=0;i<=n-1;i++)
- {
- printf("%d\n",a[i]);
- }
- }
Posted by
snehil
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- main()
- {
- int a[100],n,k,i,j,temp;
- printf("How many elements: \n");
- scanf("%d",&n);
- printf("Enter the element of array: \n");
- for(i=0;i<=n-1;i++)
- scanf("%d",&a[i]);
- for(k=1;k<=n-1;k++)
- {
- temp=a[k];
- j=k-1;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- while((temp<a[j])&&(j>=0))
- {
- a[j+1]=a[j];
- j=j-1;
- }
- a[j+1]=temp;
- }
- printf("Elements of array after sorting: \n");
- for(i=0;i<=n-1;i++)
- printf("%d\n",a[i]);
- getch();
- }
Posted by
snehil
Thursday, July 16, 2009
Size of array is taken from user.. Elemnts are entered by user.. Elements are sorted in ascending order..
- #include<conio.h>
- #include<iostream.h>
- void main()
- {
- clrscr();
- int a[100],i,n,j,temp;
- cout<<"How many element: ";
- cin>>n;
- cout<<"Enter the elements of array: "<<endl;
- for(i=0;i<n;i++)
- cin>>a[i];
- cout<<"The elements of array Before Sorting: "<<endl;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<n;i++)
- cout<<a[i]<<" ";
- cout<<endl;
- for(i=0;i<n;i++)
- {
- for(j=0;j<n-1-i;j++)
- {
- if(a[j]>a[j+1])
- {
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- }
- }
- }
- cout<<"Elements of array After Sorting: "<<endl;
- for(i=0;i<n;i++)
- cout<<a[i]<<" ";
- getch();
- }