简单排序——冒泡排序,选择排序,插入排序,对象排序
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1)冒泡排序
package sort;
/**
?* 冒泡排序,每次把數(shù)組最大值送到未排序部分的最末端
?* @author Administrator
?*
?*/
public class BubbleSort {
/**
* 輸入:無(wú)序數(shù)組
* 輸出:有序數(shù)組
* length代表數(shù)組的實(shí)際長(zhǎng)度
*/
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;
/**
?* 選擇排序,每次選擇未排序數(shù)組最小值跟未排序的最前端進(jìn)行交換,交換后將之加入已排序部分
?* @author Administrator
?*
?*/
public class SelectSort {
/**
* 輸入:無(wú)序數(shù)組
* 輸出:有序數(shù)組
*/
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;
/**
?* 插入排序,建立前段為已排序部分(當(dāng)然剛開(kāi)始時(shí)是第一個(gè)元素),然后從已排序后面的第一個(gè)元素作為插入元素插到已排序部分
?* 插入方法是其前一個(gè)元素占用它的存儲(chǔ)空間,以此類(lèi)推
?* @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 )對(duì)象排序 package sort;
/**
?* 對(duì)對(duì)象元素進(jìn)行排序,使用對(duì)象數(shù)組進(jìn)行,核心排序方法使用插入排序思想,簡(jiǎn)單易實(shí)現(xiàn)還挺便捷。
?* @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 );
}
}
轉(zhuǎn)載于:https://my.oschina.net/DanielLee/blog/189670
總結(jié)
以上是生活随笔為你收集整理的简单排序——冒泡排序,选择排序,插入排序,对象排序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux常用命令--文件(夹)查找之f
- 下一篇: Linux C Socket编程发送结构