日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

简单排序——冒泡排序,选择排序,插入排序,对象排序

發布時間:2024/4/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单排序——冒泡排序,选择排序,插入排序,对象排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

1)冒泡排序

package sort;



/**
?* 冒泡排序,每次把數組最大值送到未排序部分的最末端
?* @author Administrator
?*
?*/
public class BubbleSort {
/**
* 輸入:無序數組
* 輸出:有序數組
* length代表數組的實際長度
*/
public int[] bubbleSort(int[] arrayNum,int length)
{
for(int i = length - 2 ; i >= 0; i --)
for(int j = 0; j <= i ; j ++)
{
if(arrayNum[j] > arrayNum[j+1])
{
int temp = arrayNum[j];
arrayNum[j] = arrayNum[j+1];
arrayNum[j+1] = temp;
}
}
return arrayNum;
}

public static void main(String[] args)
{
int[] arrayNum = {2,1,4,11,6,8,5};
arrayNum = new BubbleSort().bubbleSort(arrayNum,arrayNum.length);
for(int i = 0 ; i <= arrayNum.length - 1 ; i ++)
System.out.println(arrayNum[i]);
}

}


2)選擇排序

package sort;


/**
?* 選擇排序,每次選擇未排序數組最小值跟未排序的最前端進行交換,交換后將之加入已排序部分
?* @author Administrator
?*
?*/
public class SelectSort {
/**
* 輸入:無序數組
* 輸出:有序數組
*/
public int[] selectSort(int[] arrayNum, int length)
{
for(int i = 0 ; i <= length - 2; i ++)
for(int j = i+1; j <= length - 1; j++)
{
if(arrayNum[j] < arrayNum[i])
{
int temp = arrayNum[i];
arrayNum[i] = arrayNum[j];
arrayNum[j] = temp;
}
}
return arrayNum;
}

public static void main(String[] args)
{
int[] arrayNum = {2,1,4,11,6,8,5};
arrayNum = new SelectSort().selectSort(arrayNum,arrayNum.length);
for(int i = 0 ; i <= arrayNum.length - 1 ; i ++)
System.out.println(arrayNum[i]);
}
}

3)插入排序

package sort;
/**
?* 插入排序,建立前段為已排序部分(當然剛開始時是第一個元素),然后從已排序后面的第一個元素作為插入元素插到已排序部分
?* 插入方法是其前一個元素占用它的存儲空間,以此類推
?* @author Administrator
?*
?*/
public class InsertionSort {
public int[] insertionSort(int[] arrayNum,int length)
{
for(int sortedFlag = 1; sortedFlag <= length - 1; sortedFlag ++)
{
int temp = arrayNum[sortedFlag];
for(int insertFlag = 0 ; insertFlag <= sortedFlag - 1 ; insertFlag ++)
{
if(arrayNum[insertFlag] >= arrayNum[sortedFlag] )
{
for(int k = sortedFlag; k >= insertFlag+1 ; k--)
{
arrayNum[k] = arrayNum[k-1];
}
arrayNum[insertFlag] = temp;
}
}
}
return arrayNum;
}

public static void main(String[] args)
{
int[] arrayNum = {2,1,4,11,6,8,5};
arrayNum = new InsertionSort().insertionSort(arrayNum,arrayNum.length);
for(int i = 0 ; i <= arrayNum.length - 1 ; i ++)
System.out.println(arrayNum[i]);
}
}


4 )對象排序

package sort;
/**
?* 對對象元素進行排序,使用對象數組進行,核心排序方法使用插入排序思想,簡單易實現還挺便捷。
?* @author Administrator
?*
?*/
class Person
{
int id;
String label;

public Person(int id,String label)
{
this.id = id;
this.label = label;
}
}


public class ObjectSort {
public Person[] insertionObjectSort(Person[] personList, int length)
{
for(int sortFlag = 1; sortFlag <= length -1 ; sortFlag ++)
for(int insertFlag = 0; insertFlag <= sortFlag - 1 ; insertFlag ++)
{
Person tempPerson = new Person(personList[sortFlag].id,personList[sortFlag].label);
if(personList[sortFlag].id < personList[insertFlag].id)
{
for(int k = sortFlag; k >= insertFlag + 1 ; k --)
personList[k] = personList[k-1];
personList[insertFlag] = tempPerson;
}
}
return personList;
}

public static void main(String[] args)
{
Person[] personList = {new Person(3,"ZHAO"),new Person(8,"QIAN"),new Person(1,"SUN"),new Person(6,"LI"),new Person(11,"ZHOU"),new Person(4,"WU"),new Person(6,"ZHENG")};
personList = new ObjectSort().insertionObjectSort(personList,personList.length);
for(int i = 0 ; i <= personList.length - 1; i ++ )
System.out.println("ID = " + personList[i].id + ",LABEL = " + personList[i].label );
}
}



轉載于:https://my.oschina.net/DanielLee/blog/189670

總結

以上是生活随笔為你收集整理的简单排序——冒泡排序,选择排序,插入排序,对象排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。