Sorting and Finding methods

Source: Internet
Author: User
Tags goto printf sort

#include <malloc.h>
#include <stdio.h>
#define N 11
* * Use surveillance whistle to find * *
int search (int array[],int n,int k)
{int i;
i=n-1;
Array[0]=k;
while (array[i]!=k) i--;
return (i);
}
/* Binary Lookup method * *
int halfsearch (int array[],int n,int k)
{int i,j,mid;
I=1;j=n;
while (I&LT;=J)
{mid= (I+J)/2;
if (K==array[mid]) return (mid);
else if (K<array[mid]) j=mid-1;
else i=mid+1;
}
return (0);
}

/* Bubble Sort Method * *
void Mpsort (int array[])
{int i,j,a;
a=0;
for (i=1;i<n;i++)
for (j=i+1;j<n;j++)
if (Array[i]>array[j])
{A=array[i];
ARRAY[I]=ARRAY[J];
Array[j]=a;}
}
/* Direct Insert Sort * *
void Insertsort (int array[])
{int i,j;
for (i=2;i<n;i++)
{Array[0]=array[i];
J=i-1;
while (Array[0]<array[j])
{array[j+1]=array[j--];
ARRAY[J+1]=ARRAY[0];
}
}
}
/* Establish * *
void creat (int array[])
{int i;
printf ("Enter the array:\n");
for (i=1;i<n;i++)
scanf ("%d", &array[i]);
}

/* Display * *
void print (int array[])
{int i;
printf ("The numbers after sort is:\n");
for (i=1;i<n;i++)
printf ("%d", array[i]);
printf ("\ n");
}

Main ()
{int A[11],i,x,chang;
/*printf ("Enter the array\n");
for (i=1;i<11;i++)
scanf ("%d", &a[i]);

* www.bianceng.cn * *

Aga
printf ("\nchang:1: Use watching method finding\n 2:use half method finding\n 3:use directness Intsert method sort\n 4:use Bubble up Method sort\n 5:exit\n ");
scanf ("%d", &chang);
Switch (Chang)
{Case 1:
{creat (a);
printf ("Please int the search number:\n");
scanf ("%d", &x);
printf ("The number station is:%d\n", Search (a,n,x));
Goto Aga;
}
Case 2:
{creat (a);
Insertsort (a);
Print (a);
printf ("Please int the search number:\n");
scanf ("%d", &x);
printf ("The number station is:%d\n", Halfsearch (a,n,x));
Goto Aga;
}
Case 3:
{creat (a);
Insertsort (a);
Print (a);
Goto Aga;
}

Case 4:
{creat (a);
Mpsort (a);
Print (a);
Goto Aga;
}

Case 5:{printf ("exit!\n");
default:{printf ("error!\n"); Goto Aga;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.