2013年12月11日 星期三

選擇排序法 (Selection sort)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
 int a[10] = {86,46,62,71,17,3,7,25,44,17};
 int x = 0,j = 0;


 for(int i=1;i<10;i++)
 {
  x = a[i];

  for(j=i-1;j>=0;j--)
  {
   if(a[j]>x)
    a[j+1] = a[j];

   else
   {
    a[j+1] = x;
    break;
   }
  }
  
  if(j < 0)
   a[0] = x;
 }

 for(int i=0;i<10;i++)
  cout<<a[i]<<" ";

 system ("pause");
 return 0;
}

沒有留言:

張貼留言