用JAVA编写插入法对一个给定数组进行升序排序的方法

来源:百度知道 编辑:UC知道 时间:2024/06/23 08:32:38
该方法为:“static void insersort(int arr[])”

//用冒泡,就是for循环里加if判断就行了。
class Test{
public static void main(String [] args){
int a[10]={2,1,4,5,6,7,8,9,23,44};
for (i=0;i<a.length;i++)
{
for (j=0;j<a.length-1-i;j++)
{
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for (i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
}

/* Snippet: Insert Sort
* Author: SPlutard
*
*Description: Sorts an array of integers using the insertion sort algorithm (moving each element to its proper place).
* More efficient than Bubble Sort.
*/

public static void insertionSort(int[] list, int length) {
int firstOutOfOrder, location, temp;

for(firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++) { //Starts at second term, goes until the end of the array.
if(list[firstOutOfOrder] < list[f