public class 选择排序和冒泡排序
{ /**
 * @param args
 */
public static void main(String[] args)
{
int arr[] =
{ 4, 3, 2, 1, 8, 7, 6, 5, 9 }; // 选择排序;
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr.length - i; j++)
{
if (arr[i] > arr[j])
{
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
}
} for (int temp : arr)
System.out.print(temp + " "); }
}

解决方案 »

  1.   

    public class 选择排序和冒泡排序
    { /**
     * @param args
     */
    public static void main(String[] args)
    {
    int arr[] =
    { 4, 3, 2, 1, 8, 7, 6, 5, 9 };
    int temp = 0;
    // 选择排序;
    for (int i = 0; i < arr.length; i++)
    {
    for (int j = i; j < arr.length; j++)
    {
    if (arr[i] > arr[j])
    { temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    System.out.println(temp + " " + arr[i] + "  " + arr[j]);
    /*
     * arr[i] = arr[i] + arr[j]; arr[j] = arr[i] - arr[j];
     * arr[i] = arr[i] - arr[j];
     */ }
    }
    } for (int to : arr)
    System.out.print(temp + " "); }
    }
    这样也不行.请问那里出错了???
      

  2.   

    你去看看 Arrays.sort()的实现吧。    int arr[] = { 4, 3, 2, 1, 8, 7, 6, 5, 9 };
        Arrays.sort(arr);    for (int to : arr)
          System.out.print(to + " ");这里应该用 to 而不是 temp
      

  3.   


    public static void main(String[] args) 

    int arr[] = 
    { 4, 3, 2, 1, 8, 7, 6, 5, 9 };  // 选择排序; 
    for (int i = 0; i < arr.length; i++) 

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

    if (arr[j+1] > arr[j]) 

    arr[j+1] = arr[j+1] + arr[j]; 
    arr[j] = arr[j+1] - arr[j]; 
    arr[j+1] = arr[j+1] - arr[j]; 


    }  for (int temp : arr) 
    System.out.print(temp + " ");  } 不是i和j交换,是j和j+1
      

  4.   


    public class Sort
    {
        /**
         * @param args
         */
        public static void main(String[] args)
        {
            int arr[] = {4, 3, 2, 1, 8, 7, 6, 5, 9};
            boolean flag = true;
            int n = arr.length;
            int temp = 0;
            
            for(int i = 1; i < n && flag; i++)
            {
                flag = false;
                
                for(int j = 0; j < n - i; j++)
                {
                    if(arr[j] > arr[j + 1])
                    {
                        flag = true;
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                        System.out.println(temp + " " + arr[i] + "  " + arr[j]);
                    }
                }
            }        for(int to: arr)
            {
                System.out.print(to + " ");
            }    }
    }
      

  5.   

    你是每次从待排序的数里面选一个最小出来放在衣排序的后面 对吧你的第二种写法是对的只是循环输出写错了 for (int to : arr)
                System.out.print(temp + " "); //怎么能一直输出temp呢 要输出to
      

  6.   

    汗 又重复答了 csdn的延迟太久了
      

  7.   

    呵呵,挺搞的。for (int to : arr)
                System.out.print(to + " ");
      

  8.   

    lz的问题是算法上的问题。
    不是i和j交换,而是j和j+1交换
    for (int to : arr) 
         System.out.print(temp + " "); 
    这样的话是编译不通过,并不是结果不正确
      

  9.   


    public static void main(String[] args) 

    int arr[] = 
    { 4, 3, 2, 1, 8, 7, 6, 5, 9 }; 

    int maxPos;
    int temp; // 选择排序; 
    for (int i = 0; i < arr.length; i++) 

    maxPos = i;
    for (int j = i; j < arr.length-1; j++) 

    if (arr[j+1] > arr[j]) 

    maxPos = j+1;


    temp = arr[i];
    arr[i] = arr[maxPos];
    arr[maxPos] = temp;

    }  for (int to : arr) 
    System.out.print(to + " ");  }这样就是选择排序了
      

  10.   

    public class 选择排序和冒泡排序 
    { /** 
    * @param args 
    */ 
    public static void main(String[] args) 

    int arr[] = 
    { 4, 3, 2, 1, 8, 7, 6, 5, 9 }; // 选择排序; 
    for (int i = 0; i < arr.length; i++) 

     for (int j = i; j < arr.length; j++){ 
    if (arr[i] > arr[j]) 

    arr[i] = arr[i] + arr[j]; 
    arr[j] = arr[i] - arr[j]; 
    arr[i] = arr[i] - arr[j]; 


    } for (int temp : arr) 
    System.out.print(temp + " "); } 
    } 这样不就可以吗
      

  11.   


                    int arr[] ={ 4, 3, 2, 1, 8, 7, 6, 5, 9 };
            int temp = 0;   //记录数组下标用的变量
            for(int i=0;i<arr.length;i++)
            {
             temp = i;
             for(int j=i+1;j<arr.length;j++)
             {
            
             if(arr[temp]>arr[j])
             {
             temp = j; //取其小的数,再让这个小的数去跟后面的数比             
             }
             }
             int n = arr[i];
             arr[i]=arr[temp];
             arr[temp] = n;
             System.out.println(arr[i]);        
            }/*冒泡是比较一次,取其大或小,就交换一次。
      选择排序的思路是,在冒泡的基础上,不是比N次就交换N次。是比较一次之后记录那个大的或小的那个数的数组下标。
      循环完一轮在交换/*/
      
      

  12.   

    全都写出来了.谢谢大家.public class 选择排序和冒泡排序
    { /**
     * @param args
     */
    public static void main(String[] args)
    {
    int arrtest[] =
    { 3, 2, 1, 6, 5, 4, 9, 8, 7 };
    int[] arr = 选择排序(arrtest);
    showarry(arrtest);
    System.out.println();
    int arrtest1[] =
    { 3, 2, 1, 6, 5, 4, 9, 8, 7 };
    int[] arr1 = 冒泡排序(arrtest1);
    showarry(arrtest);
    } /**
     * 输出数组;
     */
    private static void showarry(int[] arr)
    {
    for (int to : arr)
    System.out.print(to + " ");
    } /**
     * 选择排序
     */
    public static int[] 选择排序(int[] arr)
    { int temp = 0;
    // 选择排序;
    for (int i = 0; i < arr.length; i++)
    {
    for (int j = i; j < arr.length; j++)
    {
    if (arr[i] > arr[j])
    { temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp; }
    }
    }
    return arr;
    } public static int[] 冒泡排序(int[] arr)
    { int temp = 0;
    // 选择排序;
    for (int i = 0; i < arr.length; i++)
    {
    for (int j = i; j < arr.length - 1; j++)
    {
    if (arr[j + 1] < arr[j])
    {
    temp = arr[j];
    arr[j] = arr[j + 1];
    arr[j + 1] = temp; }
    }
    }
    return arr;
    }
    }
      

  13.   


    for (int i = 0; i < arr.length; i++)
            {
                for (int j = i; j < arr.length; j++)
                {
                    if (arr[i] > arr[j])
                    {                    temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;                }
                }
            }
    老毛病还是没改啊,交换次数太多了.要用一个临时变量记录要交换的数的位置,最后才交换int maxPos;
            int temp;        // 选择排序; 
            for (int i = 0; i < arr.length; i++) 
            { 
                maxPos = i;
                for (int j = i; j < arr.length-1; j++) 
                { 
                    if (arr[j+1] > arr[j]) 
                    { 
                        maxPos = j+1;
                    } 
                } 
                temp = arr[i];
                arr[i] = arr[maxPos];
                arr[maxPos] = temp;
                
            } 
      

  14.   

    楼主的算法都是比较一次就把小数换到前面去,都是冒泡系列的。
    选择排序是记录数组下标,核心在这里                temp = i;       //每次大循环时,让temp先等于i
                    for(int j=i+1;j<arr.length;j++)
                    {
                        
                        if(arr[temp]>arr[j])  //不是arr[i]跟arr[j]比,而是arr[temp]跟arr[j]比较
                          {
                            temp = j;         //使temp始终为相对的小的那个数的数组下标                   
                          }
                    }