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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值