您的位置:首页 > 其它

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

2014-01-02 22:01 330 查看
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 );

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐