A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 许大虾 中级黑马   /  2013-5-19 11:37  /  2979 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. int[] scores={1,2,3,4,5,6,7,8,9};
  2. //定义一个数组
  3. for(int i=0;i<scores.length -1;i++)
  4. {
  5. for(int j=0;j<scores.length -1 -i;j++)
  6. {
  7. if(scroes[j]<scores[j+1])
  8. {
  9. int temp=scores[j];
  10. scores[j]=scores[j+1];
  11. scores[j+1=temp;
  12. }
  13. }
  14. }
  15. for(int i=0;i<scores.length;i++)
  16. {
  17. console.writeline(scores[i]);
  18. }
  19. console.readkey();
复制代码
if(scroes[j]<scores[j+1])
{
int temp=scores[j];
scores[j]=scores[j+1];
scores[j+1=temp;
这一句是什么意思?
还有里面的
for(int j=0;j<scores.length -1 -i;j++)
   j是什么意思

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

13 个回复

倒序浏览
本帖最后由 李君言 于 2013-5-19 14:09 编辑
  1. if(scroes[j]<scores[j+1])
  2. {
  3. int temp=scores[j];
  4. scores[j]=scores[j+1];
  5. scores[j+1]=temp;
复制代码
如果数组里的第j的元素小于第j+1的元素(就是前面那个小于后那个)的时候
交换两个的值

点评

哥们你最后一句的中括号打错地儿了  发表于 2013-5-19 12:39

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
冒泡算法的理解就是,从第一个相比,没两两相比计较,如果大或者小就交换,两层循环,第一层是控制比较的轮数,第二层是比较的次数,每次两两相比。j用来表示数组的下标,用在score[j]就是元素了,
if(scroes[j]<scores[j+1])
{
int temp=scores[j];
scores[j]=scores[j+1];
scores[j+1=temp;
}
这个就是如何第一个数比第二个数小,就交换

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
噗  lz这句少了个]
scores[j+1=temp;
这里的J表示下标  因为你在第i次的需要比较scores.length -i次  然后把大的往前提

int temp=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
scores[j+1=temp;
这一句是什么意思?
这一句可以编译通过?????
回复 使用道具 举报
if(scroes[j]<scores[j+1])//这是如果j索引下的那个数小于j+1索引下的那个数就交换位置。
{
int temp=scores[j];//把j索引对应的数赋值给temp;j就为空了
scores[j]=scores[j+1];//再把j+1索引下的数赋值给j,j索引下的数现在是j+1的了;
scores[j+1]=temp;//再把temp里的数就是j索引 下的数赋值给j+1.j+1索引下就是j的数了。整个的这不就实现了交换位置。
这一句是什么意思?
还有里面的
for(int j=0;j<scores.length -1 -i;j++)//这个就是这个冒泡排序的循环次数,自己不和自己比,所以是length长度-1次啊
    j是什么意思

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
temp是第三方变量, 相当于一个空盒子, 你把scores[j]的值放在temp里边, 再把scores[j+1]的值放在scores[j]里边, 最后把scores[j]的值放在scores[j+1]里边. 一圈换下来, scores[j]和scores[j+1]的值就相当于调换了一个位置 , 明白?
还有你的代码好乱,给你贴一个标准的冒泡排序法:
  1. int[] arr = new int[] { 6, 1, 2, 7, 2, 5, 1, 9, 8, 5, 0, 8, 0, 3, 0, 0, 1, 4 };
  2.             foreach (int m in arr)
  3.                 Console.Write(m + " ");
  4.             Console.WriteLine();

  5.             int j, temp;
  6.             for (int i = 0; i < arr.Length - 1; i++)
  7.             {
  8.                 j = i + 1;
  9.             id:
  10.                 if (arr[i] > arr[j])
  11.                 {
  12.                     temp = arr[i];
  13.                     arr[i] = arr[j];
  14.                     arr[j] = temp;
  15.                     goto id;
  16.                 }
  17.                 else
  18.                     if (j < arr.Length - 1)
  19.                     {
  20.                         j++;
  21.                         goto id;
  22.                     }
  23.             }

  24.             foreach (int n in arr)
  25.                 Console.Write(n + " ");
  26.             Console.WriteLine();
  27.             Console.ReadKey();
复制代码

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报

int [] array = new int [*] ;//放上你的数组。
int temp = 0 ;
for (int i = 0 ; i < array.Length - 1 ; i++)
{
for (int j = i + 1 ; j < array.Length ; j++)
{
if (array[j] < array[i])
{
temp = array[i] ;
array[i] = array[j] ;
array[j] = temp ;
}
}
}
回复 使用道具 举报
李君言 发表于 2013-5-19 11:42
如果数组里的第j的元素小于第j+1的元素(就是前面那个小于后那个)的时候
交换两个的值 ...

谢谢~谢谢
回复 使用道具 举报
贡宗新 发表于 2013-5-19 11:43
冒泡算法的理解就是,从第一个相比,没两两相比计较,如果大或者小就交换,两层循环,第一层是控制比较的轮 ...

谢谢谢谢~`
回复 使用道具 举报
曾大鹏 发表于 2013-5-19 11:47
噗  lz这句少了个]
scores[j+1=temp;
这里的J表示下标  因为你在第i次的需要比较scores.length -i次  然后 ...

谢谢谢谢谢谢~~~
回复 使用道具 举报
int[] scores={1,2,3,4,5,6,7,8,9};
//定义一个数组
for(int i=0;i<scores.length -1;i++)//总共要需要排序的数据为length个
{
for(int j=0;j<scores.length -1 -i;j++)//总共要比较(length -1 -i )次
{
if(scroes[j]<scores[j+1])//scroes[j],scores[j+1])2值比较通过过度变量temp将小的值存放到score[j]
{
int temp=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;
}
}
}
for(int i=0;i<scores.length;i++)//输出排序完成后的数组结果
{
console.writeline(scores[i]);
}
console.readkey();
回复 使用道具 举报
冒泡排序记住一句话:嵌套循环 外层循环N-1次(趟数),N代表有多少个数,内层循环N-1-i次(比较次数)
回复 使用道具 举报
两两比较。int[] num={ }

原始数据: 1,2,3,4,5,6,7,8,9,10 十个元素(从大到小排列)----需要比较10-1趟

          2,3,4,5,6,7,8,9,10,1 第一趟比较9次。(10-1)          num[0]是第一趟。

          3,4,5,6,7,8,9,10,2,1 第二趟比较8次。(10-1-1)        num[1]是第二趟。

          4,5,6,7,8,9,10,3,2,1 第三趟比较7次。  ……

          5,6,7,8,9,10,4,3,2,1 第四趟比较6次。

          6,7,8,9,10,5,4,3,2,1 第五趟比较5次。

          7,8,9,10,6,5,4,3,2,1 第六趟比较4次。

          8,9,10,7,6,5,4,3,2,1 第七趟比较3次。

          9,10,8,7,6,5,4,3,2,1 第八趟比较2次。

          10,9,8,7,6,5,4,3,2,1 第九趟比较1次。


程序:

        int[] numbers={1,2,3,4,5,6,7,8,9,10};
        for(int i=0;i<numbers.Length-1;i++)//控制趟数,循环一次就是一趟。       
        {
           for(int j=0;j<numbers.Length-1-i;j++)
             {
                if(numbers[j]<numbers[j+1])
                {
                   int temp=numbers[j];
                   numbers[j]=numbers[j+1];
                   numbers[j+1]=temp;
                }
             }       
        }
       
        for(int i=0;i<numbers.length;i++)
        {
                console.writeLine(numbers[i]);
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马