您的位置 电脑知识爱好者 >> 编程知识 >> 什么叫做插入排序!

什么叫做插入排序!

电脑知识爱好者互联网本站整理2007-10-4 0:25:59
知识重点:什么叫做插入排序 请举例说明 数据结构上的4种简单排序算法 //数据结构上的4种简单排序算法。 /** * pTitle: * /ppDescription: /p * pCopyright: Copyright (c) 2005/p * pCompany: /p * @auth..

{

if (s[in]>s[in+1])

{

swap(in,in+1);

}

}

}

for (int j =0;j< a;j++)

{

System.out.println(j+"="+s[j]);

}

}

private void swap(int one ,int two)

{

int temp = s[one];

s[one] = s[two];

s[two] = temp;

}

public static void main(String[] args)

{

Sort in = new Sort();

//in.insort();

in.selectSort();

// in.bubbleSort();

}

}

4、快速排序

先取数组的中间元素,并将数组分为两个较小的数组,其中一个数组只含大于这个中间值的元素,而另一个数组只含小于这个中间值的元素,重复这一过程。

public class arrayQsort{

static void qsort(int array[],int first,int last){

int low=first;

int high=last;

if(first>=last) return;

int mid=array[(first+last)/2];

do{

while(array[low]< mid)

low++;

while(array[high]>mid)

high--;

if(low<=high){

int temp=array[low];

array[low++]=array[high];

array[high--]=temp;

}

}while(low<=high);

qsort(array,first,high);

qsort(array,low,last);

}

public static void main(String[] args){

int[] s = {12,4,56,44,3,34,32,2,5,21};

qsort(s,0,9);

for (int j =0;j<10;j++)

{

System.out.println(j+"="+s[j]);

}

}

}

程序运行的一个结果:

参与评论
相关内容
关于我们 | 隐私政策 | 站点地图 | 站长博客|京ICP备07025396号
添加到百度搜藏 添加到百度搜藏 电脑知识爱好者Copyright ?2006-2008版权所有 我要啦免费统计