JAVA学习日记----------排序篇01

JAVA学习日记----------排序篇01

1.排序的概念:将一组数据按照相应的规则排列顺序:可以按照大小,可以按照时间前后,或者根据不同的业务要求排序

2.排序算法:冒泡,快排,二分法,选择,插入shell等

冒泡排序:存在序列an,升序排列

an和an+1比,将大的数放在后面,依次类推,排完一次的结果是,最大的数会在序列的最后面(排n-1次)

然后循环n次实现升序排列,算法实现如下:

public static void main(String [] args){

int[] a = new int[n];

for(int i = 0;i<a.length-1;i++){

for(int j = 0;j<a.length-i-1;j++){

if(a[i]>a[i+1]){

int temp =a[i];

a[i] = a[i+1];

a[i+1] = temp;

}

}

}

}

算法优化,假设数据已经有序,或者经过某趟后有序,减少趟数

如:8 1 2 3 4

经过一次就已经完成12348就不需要继续了

优化算法如下:

public static void main(String [] args){

int[] a = new int[n];

boolean sorted = true;

for(int i = 0;i<a.length-1;i++){

sorted = true;

for(int j = 0;j<a.length-i-1;j++){

if(a[i]>a[i+1]){

int temp =a[i];

a[i] = a[i+1];

a[i+1] = temp;

sorted = false;

}

}

if(sorted){

break;

}

}

}


今天的TopCoder练习题如下:

SRM 307 DIV2 250

roblem Statement

 

A boot shop has received a shipment from the factory consisting of N left boots and N right boots. Each boot has some integer size, and a left and right boot will form a proper pair if they have equal sizes. Each boot can only belong to a single pair. The employees of the boot store want to create N proper pairs of boots. Fortunately, the factory has offered to exchange any number of boots in the shipment with new boots of different sizes.

You are given a int[] left and a int[] right containing the sizes of the left boots and right boots, respectively. Return the least number of boots that must be exchanged.

Definition

 
Class:BootsExchange
Method:leastAmount
Parameters:int[], int[]
Returns:int
Method signature:int leastAmount(int[] left, int[] right)
(be sure your method is public)

我写的代码如下:

public class BootsExchange {
public int leastAmount(int[]left,int[]right){
int LeastNumber=left.length;
if(left.length!=right.length){
System.out.println("Input is illegal");
return -1;
}
else{
for(int i =0;i<left.length;i++){
for(int j= 0;j<right.length;j++){
if(right[i]==left[j]){
LeastNumber--;
break;
}
}
}
return LeastNumber;
}
}

}

代码缺陷,如果出现这样的测试数据,代码会出现问题

left{2,1,3}

right{1,3,1}

出现问题的原因是在right中出现了相同的元素,没有做判断,会多减一次,我的想法是,用Left作为被比较的数组的话,判断被比较的数组,如果有重复的元素仅+1一次无论重复多少数


SRM 307 DIV2 500

Problem Statement

 You are given a int[] data. All elements in data are distinct. Only consecutive pairs of elements in data can be swapped. You are allowed to perform at most nSwaps swaps. Return the lexicographically maximal possible result.

Definition

 
Class:PartSorting
Method:process
Parameters:int[], int
Returns:int[]
Method signature:int[] process(int[] data, int nSwaps)
(be sure your method is public)

我写的代码如下:

public class PartSorting{
public int [] process(int[]data,int nSwaps) {
if(nSwaps>data.length/2){try{
throw  new Exception("Your Input Is Illegal");
}catch(Exception e){
e.printStackTrace();
return null;
}

}else{
for(int i = 0;i<nSwaps;i++){
int temp = data[2*i];
data[2*i]=data[2*(i+1)-1];
data[2*(i+1)-1]=temp;
}
return data;
}
}

}

不足:Try catch需要更加了解写法



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值