`
windshjw
  • 浏览: 41209 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

java算法

阅读更多
package sortTest;

public class BubbleTest
{

/**
* @param args
*/
public static void main(String[] args)
{
int[] num = new int[]{ 3, 1, 5, 4, 2, 6, 0, 9 };
int[] result = sort(num);
for(int i = 0 ; i< result.length;i++){
System.out.println(result[i]);
}


}

public static int[] sort(int[] num){
for(int i =0; i< num.length-1 ; i++){
for(int j=0; j<num.length-1-i;j++){
int temp = 0;
if(num[j+1]<num[j]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}


}

return num;
}

}


package sortTest;

public class InsertSortTest
{

/**
* @param args
*/
public static void main(String[] args)
{
int[] num = new int[]
{ 3, 1, 5, 4, 2, 6, 0, 9 };
insertSort(num);
for(int i = 0 ; i< num.length;i++){
System.out.println(num[i]);
}
}

public static int[] insertSort(int[] num){
int j = 0;
for(int i =0 ; i< num.length -1; i++){
j = i;
int temp = num[i];

for(;j>0; j--){
if(num[j-1]>temp){
num[j]=num[j-1];
}else{
break;
}

}
num[j]=temp;

}


return num;
}
}


package sortTest;

public class QuickSortTest
{

/**
* @param args
*/
public static void main(String[] args)
{
int[] num = new int[]{ 3, 1, 5, 4, 2, 6,0,9 };
int[] result = quickSort(num, 0, num.length - 1);
for(int i = 0 ; i< result.length;i++){
System.out.println(result[i]);
}
}

public static int[] quickSort(int[] num, int left, int right)
{
if (left < right)
{
int singleSort = singleSort(num, left, right);

quickSort(num, left, singleSort-1);
quickSort(num, singleSort+1, right);
}

return num;
}

public static int singleSort(int[] num, int left, int right)
{
int x = num[left];
int i = left;
int j = right;
while (i < j)
{
while (i < j && num[j] >= x)
{
j--;
}
num[i] = num[j];
while (i < j && num[i] <= x)
{
i++;
}
num[j] = num[i];
}
num[i]=x;

return i;
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics