当前位置:编程学习 > JAVA >>

冒泡排序

对字符串“100,10,1,0,15,20”进行冒泡排序 --------------------编程问答-------------------- 网上一搜一大把,发个帖子问够浪费的 --------------------编程问答--------------------

先转换为整形数组吧
public static void main(String[] args) {
int[] array = { 100, 10, 1, 0, 15, 20 };
sort(array);
for (int el : array) {
System.out.print(el + " ");
}
}

static void sort(int[] array) {
int length = array.length;
int temp;
boolean isSort;
for (int i = 1; i < length; i++) {
isSort = false;
for (int j = 0; j < length - i; j++) {
if (array[j] > array[j + 1]) {

temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
isSort = true;
}
}
if (!isSort)
break;
}
}
--------------------编程问答-------------------- 网上的找了好多不标准呀 --------------------编程问答-------------------- 最好言简意赅、一目了然的 --------------------编程问答-------------------- int arr [] = {100,10,1,0,15,20};

for ( int i = 0; i < arr.length-1;i++ ) {
    for (int j = 0;j < arr.length-i;j++ ) {
         int temp = arr[j];
         arr [j] = arr[j+1];
         arr[j+1] = temp
    }

}

大概就是这个样子了。 --------------------编程问答-------------------- 把字符串转化成int[]数组 --------------------编程问答--------------------

public class Test {
public static void main(String[] args) {
//将字符串转化为整型数组
String s = "100,10,1,0,15,20";
String[] st = s.split(",");
int[] array = new int[st.length];
for(int i=0; i<st.length; i++) {
array[i] = Integer.parseInt(st[i]);
}  
bubbleSort(array) ;
}

//冒泡排序
public static void bubbleSort(int[] a) {
for(int i=0; i<a.length; i++) {
for(int j=0; j<a.length-1; j++) {
if(a[j] > a[j+1]) {
a[j] = a[j]^a[j+1];
a[j+1] = a[j]^a[j+1];
a[j] = a[j]^a[j+1];
}
}
}

for(int i=0; i<a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
--------------------编程问答--------------------
public class Test01 {
public static void main(String[] args){
String str = "100,10,1,0,15,20";
String[] s = str.split(",");
int[] a = new int[s.length];
//将字符串转换为int
for(int i=0;i<s.length;i++){
a[i] = Integer.parseInt(s[i]);
}
//冒泡排序
int temp = 0;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-i-1;j++){
if(a[j]<a[j+1]){
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"、");
}
}
}

输出结果:
100、20、15、10、1、0
--------------------编程问答-------------------- 楼上的例子都不错。 --------------------编程问答-------------------- 对字符串排序java还是比c好用 --------------------编程问答-------------------- //希望对楼主有用哈!
//对数组“100,10,1,0,15,20”进行冒泡排序
class Bubble
{
//定义一个功能,遍历数组
public static void Print(int[] arr)
{
for(int x = 0; x < arr.length; x++)
System.out.print(arr[x] + " ");
System.out.println();//为了美观,好看点
}

//定义冒泡排序,从小到大
public static void BubbleSortMax(int[] arr)
{
for(int x = 0; x < arr.length-1;x++)//arr.length-1是以为只剩下最后一个数的话不需要比较
{
for(int y = 0 ; y < arr.length-1-x ; y++)//-X是因为比较的元素在递减
{
if(arr[y] > arr[y+1])
{
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}


//定义冒泡排序,从大到小
public static void BubbleSortMin(int[] arr)
{
for(int x = 0; x < arr.length-1;x++)//arr.length-1是以为只剩下最后一个数的话不需要比较
{
for(int y = 0 ; y < arr.length-1-x ; y++)//-X是因为比较的元素在递减
{
if(arr[y] < arr[y+1])
{
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}


public static void main(String[] args)
{

int[] arr = {100,10,1,0,15,20};//定义一个新数组
System.out.println("排序前:");
Print(arr);//对未排序前的数组进行遍历

System.out.println("排序后");
BubbleSortMax(arr);//冒泡排序,从小到大
Print(arr);//对排序后的数组遍历

System.out.println("再次排序后");
BubbleSortMin(arr);//冒泡排序,从大到小
Print(arr);//对排序后的数组遍历
   }
} --------------------编程问答-------------------- 冒泡排序很简单啊,不就是两重循环吗。 --------------------编程问答--------------------
引用 楼主 zhanghangzhangyuan 的回复:
对字符串“100,10,1,0,15,20”进行冒泡排序


import java.util.Arrays;

public class BubbleSort 
{
public static void main(String[] args) 
{
String s = "100,10,1,0,15,20";
        
        String[] strs = s.split(",");
        int[] arr = new int[strs.length];
        
        for(int x=0; x<strs.length; x++)
        {
         arr[x] = Integer.parseInt(strs[x]);
        }
        
        System.out.println("排序前:"+Arrays.toString(arr));
        
        for(int i=0; i<arr.length; i++)
        {
         for(int j=0; j<arr.length-i-1; j++)
         {
         if(arr[j]>arr[j+1])
         {
         int temp = arr[j];
         arr[j] = arr[j+1];
         arr[j+1] = temp;
         }
         }
        }
        System.out.println("排序后:"+Arrays.toString(arr));
}
}

--------------------编程问答-------------------- http://blog.csdn.net/hailushijie/article/details/8785666 --------------------编程问答--------------------

package IO;
/**
 * @author Administrator
 * 这里以int为例子了。可以将字符串转换。
 */
public class BubbleSort{
      public static void main(String[] args){
          int score[] = {100,10,1,0,15,20};
          for (int i = 0; i < score.length -1; i++){    //最多做n-1趟排序
              for(int j = 0 ;j < score.length - i - 1; j++){    //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
                  if(score[j] < score[j + 1]){    //把小的值交换到后面
                      int temp = score[j];
                      score[j] = score[j + 1];
                      score[j + 1] = temp;
                  }
              }            
              System.out.print("第" + (i + 1) + "次排序结果:");
              for(int a = 0; a < score.length; a++){
                  System.out.print(score[a] + "\t");
              }
              System.out.println("");
          }
              System.out.print("最终排序结果:");
              for(int a = 0; a < score.length; a++){
                  System.out.print(score[a] + "\t");
         }
      }
  }
--------------------编程问答-------------------- 基础啊,不至于发个帖子问,动动手 网上有一搜一大把
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,