用java对象数组怎样进行冒泡排序

用java对象数组怎样进行冒泡排序,第1张

/**

* 冒泡排序

*/

public class Example6_6{

public static void main(String[] agrs){

int[] intArray=new int[]{3,0,1,18,5,0,-5,-6,3}//定义序列

int tempLength=intArray.length

for(--tempLength>0){

for(int index=0index<tempLength++index){

//如果相邻的左边元素小于右边的元素,则互换

if(intArray[index]<intArray[index+1]){

int temp=intArray[index]

intArray[index]=intArray[index+1]

intArray[index+1]=temp

}

}

}

//输入已经排好序的数组

for(int index=0index<intArray.length++index){

System.out.print(intArray[index]+"\t")

}

}

}

package Test

import java.util.Arrays

public class Demo1 {

public static void main(String[] args) {

int[] a = {2,1,3,9,7,10,8,11,17,6}

//System.out.println(Arrays.toString(a))

sortArr(a,a.length - 1,0)

System.out.println(Arrays.toString(a))

sort(a)

System.out.println(Arrays.toString(a))

}

public static void sortArr(int[] a,int i,int j){

if(j<i){

sortOne(a,i,j)

}else{

sortOne(a,--i,0)

}

}

public static void sortOne (int[] a,int i,int j){

if(i==0)return

if(a[j + 1] < a[j]){

 int temp = a[j]

             a[j] = a[j + 1]

             a[j + 1] = temp

}

sortArr(a,i,++j)

}

 public static void sort(int[] a)

    {

        int temp = 0

        for (int i = a.length - 1 i > 0 --i)

        {

            for (int j = 0 j < i ++j)

            {

                if (a[j + 1] < a[j])

                {

                    temp = a[j]

                    a[j] = a[j + 1]

                    a[j + 1] = temp

                }

            }

        }

    }

}

上面代码是从小到大排列

package Test

import java.util.Arrays

public class Demo1 {

public static void main(String[] args) {

Object[] a = {2,1,3,9,7,10,8,11,17,6}

sortArr(a,a.length - 1,0)

System.out.println(Arrays.toString(a))

Object[] b = {'a','m','s','b','h','e'}

sortArr(b,b.length - 1,0)

System.out.println(Arrays.toString(b))

}

public static void sortArr(Object[] a,int i,int j){

if(j<i){

sortOne(a,i,j)

}else{

sortOne(a,--i,0)

}

}

public static void sortOne (Object[] a,int i,int j){

if(i==0)return

if(a[j + 1] instanceof Integer){

if(Integer.valueOf(""+a[j + 1]) <Integer.valueOf(""+ a[j])){

Object temp = a[j]

             a[j] = a[j + 1]

             a[j + 1] = temp

}

}else if(a[j + 1] instanceof Character){

if(a[j + 1].toString().charAt(0) <a[j].toString().charAt(0)){

Object temp = a[j]

             a[j] = a[j + 1]

             a[j + 1] = temp

}

}

sortArr(a,i,++j)

}

//  public static void sort(int[] a)

//     {

//         int temp = 0

//         for (int i = a.length - 1 i > 0 --i)

//         {

//             for (int j = 0 j < i ++j)

//             {

//                 if (a[j + 1] < a[j])

//                 {

//                     temp = a[j]

//                     a[j] = a[j + 1]

//                     a[j + 1] = temp

//                 }

//             }

//         }

//     }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11852352.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存