1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _007_冒泡排序扩展
 7 {
 8     class Program
 9     {
10         /// <summary>
11         /// 冒泡排序法2
12         /// </summary>
13         /// <param name="sortArry"></param>
14         static void Sort(int[] sortArry)
15         {
16             bool swapped;
17             do
18             {
19                 swapped = false;
20                 for(int i=0;i<sortArry.Length-1;i++)
21                 {
22                     if (sortArry[i] > sortArry[i + 1])
23                     {
24                         int temp = sortArry[i];
25                         sortArry[i] = sortArry[i + 1];
26                         sortArry[i + 1] = temp;
27                         swapped = true;
28                     }
29 
30                 }
31             } while (swapped);
32         }
33         /// <summary>
34         /// 冒泡排序法
35         /// (双层for)
36         /// </summary>
37         /// <param name="sortArry"></param>
38         static void Sort2(int[] sortArry)
39         {
40             for (int i = 0; i < sortArry.Length - 1; i++)
41             {
42                 for (int j = i + 1; j < sortArry.Length; j++)
43                 {
44                     if (sortArry[i] > sortArry[j])
45                     {
46                         int temp = sortArry[i];
47                         sortArry[i] = sortArry[j];
48                         sortArry[j] =temp;
49                     }
50                     
51                 }
52             }
53         }
54 
55         static void Main(string[] args)
56         {
57             int[] sortArray = new int[] {123,12,3,45,5,56,65,66,9 };
58             Sort2(sortArray);
59             foreach (var temp in sortArray)
60             {
61                 Console.Write(temp+"__");
62             }
63             Console.ReadKey();
64         }
65     }
66 }

 

1.学习笔记

posted on 2019-12-23 03:14  冷榷  阅读(230)  评论(0编辑  收藏  举报