Created
January 12, 2014 12:46
-
-
Save LiShuMing/8384160 to your computer and use it in GitHub Desktop.
算法列表
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ===================================================================================== | |
* | |
* Filename: insert_sort.c | |
* | |
* Description: inset sort algorithm. | |
* | |
* Version: 1.0 | |
* Created: 2014年01月12日 19时42分20秒 | |
* Revision: none | |
* Compiler: gcc | |
* | |
* Author: lishuming (a graduate student, love programming), [email protected] | |
* Organization: RADI | |
* | |
* ===================================================================================== | |
*/ | |
#include<stdio.h> | |
#define LEN 5 | |
int a[LEN]={2,131,56,321,5}; | |
void insert_sort() | |
{ | |
int i,j,key,k; | |
for(i=0;i<LEN;i++) | |
{ | |
for(j=i+1;j<LEN;j++) | |
{ | |
if(a[j]<a[i]) | |
{ | |
key=a[j]; | |
for(k=j-1;k>=i;k--) | |
a[k+1]=a[k]; | |
a[i]=key; | |
} | |
} | |
printf("%d,%d,%d,%d,%d\n",a[0],a[1],a[2],a[3],a[4]) ; | |
} | |
printf("%d,%d,%d,%d,%d\n",a[0],a[1],a[2],a[3],a[4]) ; | |
} | |
int main() | |
{ | |
insert_sort(); | |
return 0; | |
} |
/*
- =====================================================================================
* -
Filename: quicksort.c
- Description: quicksort algorithm
-
Version: 1.0
-
Created: 2014年01月13日 10时25分13秒
-
Revision: none
-
Compiler: gcc
-
Author: lishuming (think different and do it with plearsure.), [email protected]
- Organization: RADI A401
- =====================================================================================
*/
include<stdio.h>
define LEN 8
int a[LEN]={12,42,1,4,323,53,23,112};
int partition(int start,int end)
{
int x=a[end];
int i=start,j,tmp;
j=start-1;
for(i=start;i<end;i++)
{
if(a[i]<x)
{
j=j+1;
tmp=a[j];
a[j]=a[i];
a[i]=tmp;
}
}
j=j+1;
tmp=a[end];
a[end]=a[j];
a[j]=tmp;
printf("quicksort, %d,%d,%d,%d,%d,%d,%d,%d\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
return j;
}
void quicksort(int start,int end)
{
int mid=partition(start,end);
if(mid>start)
{
quicksort(start,mid-1);
quicksort(mid+1,end);
}
}
int main()
{
quicksort(0,LEN-1);
return 0;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/*
*
*/
include<stdio.h>
define LEN 8
int a[LEN]={4,2,56,23,45,90,26,41};
void merge_sort(int start,int mid,int end)
{
int n1=mid-start+1;
int n2=end-mid;
int left[n1],right[n2];
int i,j,k;
for(i=0;i<n1;i++)
left[i]=a[start+i];
for(j=0;j<n2;j++)
right[j]=a[mid+1+j];
i=j=0;
k=start;
/*
for(k=start;k<end;k++)
{
if(left[i]>right[j]&&j<n2)
a[k]=right[j++];
else if(i<n1)
a[k]=left[i++];
}*/
while(i<n1&&j<n2)
if(left[i]<right[j])
a[k++]=left[i++];
else
a[k++]=right[j++];
while(i<n1)
a[k++]=left[i++];
while(j<n2)
a[k++]=right[j++];
}
void sort(int start,int end)
{
int mid;
if(start<end)
{
mid=(start+end)/2;
sort(start,mid);
sort(mid+1,end);
merge_sort(start,mid,end);
printf("merge, %d,%d,%d,%d,%d,%d,%d,%d\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
}
}
int main()
{
sort(0,LEN-1);
return 0;
}