Bubble Sort algorithm, the selection sort

Original link: http://www.cnblogs.com/leonn/archive/2010/04/08/1706833.html

         Following the resignation of the first front-end development engineers to participate in today's interview, originally themselves mainly engaged in the development of the background, not ready to go, nothing to each other's sincerity, or try holding the state to go, the consequences can be imagined, he was asked to name a few sorting algorithms, sorting algorithms only say, shame, done for so long to develop, even forgotten some basic things that he does still need more solid foundation of things. This is not to write the next two commonly used algorithms practice.

        Bubble sort is known an unordered set of data a [1], a [2 ], ...... a [n], which need to be in ascending order. First compare a [1] and a [2] value, when a [1] is greater than a [2] values of both the exchange is otherwise unchanged. Then compare a [2] and a [3] value, if a [2] is greater than a [3] values of both the exchange is otherwise unchanged. Then compare a [3] and a [4], and so on, and finally comparing a [n-1] and the value a [n] is. After such a treatment, a [n] is the value of this data set must be the largest. Again a [1] ~ a [n -1] a process in the same manner, the value a [n-1] must be a [1] ~ a [n -1] is the largest. Again a [1] ~ a [n -2] a process in the same manner, and so on. After a total of n-1 treated wheel a [1], a [2 ], ...... a [n] to the ascending order. Advantages: stable, known number of comparisons; disadvantages: slow, can only be moved adjacent two data, the number of times of data movement.

       Selection sort is known an unordered set of data a [1], a [2 ], ...... a [n], which need to be in ascending order. First compare a [1] and a [2] value, when a [1] is greater than a [2] values of both the exchange is otherwise unchanged. Then compare a [1] and a [3] value, when a [1] is greater than a [3] values of both the exchange is otherwise unchanged. Then compare a [1] and a [4], and so on, and finally comparing a [1] and the value a [n] is. After such a treatment, a value of [1] must be this minimum set of data. Then a [2] and a [3] ~ a [n ] in the same manner as a comparison, is a [2] value must be a [2] ~ a [n ] is the smallest. Then a [3] and a [4] ~ a [n ] in the same manner as a comparison, and so on. After a total of n-1 treated wheel a [1], a [2 ], ...... a [n] to the ascending order.

         Code is posted below:

ContractedBlock.gif ExpandedBlockStart.gif Code
 
    
the using the System;
the using the System.Text;

namespace SortArithmetic
{
public class MyClass { public static void the Main () { int [] Nums, = RandomNums ( . 3 , 27 ); ShowNums ( " source array is: " , Nums,); // ShowNums ( "sorted is:", the BubbleSort (Nums,)); ShowNums ( " sorted is: " , SelectSort (Nums,)); Console.ReadLine (); } #region generating a random array int Private static int











[] RandomNums( int Length, int Count)
{
Random Rand
= new Random( 7 );
int [] Nums = new int [Count];
for ( int c = 0 ; c < Count; c ++ )
{
string Result = string .Empty;
for ( int i = 0 ; i < Length; i ++ )
{
Result
+= Rand.Next( 10 ) .ToString ();
}
Nums, [C]
= Convert.ToInt32 (the Result);
}
return Nums,;
}
#endregion #region sorting algorithms: Bubble Sort (ascending order) frequency calculated: int [n], n is then 5 *. 6 (6-1) / 2 Private static int [] the BubbleSort ( int [] Nums,) { for ( int C = 0 ; C < Nums.Length - . 1 ; C ++ ) { for ( int I = 0 ; I < Nums.Length - C -







1 ; i ++ )
{
int Temp = Nums[i + 1 ];
if (Nums[i] > Nums[i + 1 ])
{
Nums[i
+ 1 ] = Nums[i];
Nums[i]
= Temp;
}
}
}
return Nums;
}
#endregion

#region 排序算法:选择排序(升序)
private static int [] SelectSort( int [] Nums)
{
for ( int c = 0 ; c < Nums.Length - 1 ; c ++ )
{
for ( int i = c; i < Nums.Length - 1 ; i ++ )
{
int Temp = Nums[i + 1 ];
if (Nums[c] > Nums[i + 1 ])
{
Nums[i
+ 1 ] = Nums[c];
Nums[c]
= Temp;
}
}
}
return Nums;
}
#endregion

private static void ShowNums( string Msgs, int [] Nums)
{
Console.Write(Msgs);
foreach ( int n in Nums)
{
Console.Write(n.ToString()
+ " " );
}
Console.WriteLine();
}


}
}

 

 

 

Reproduced in: https: //www.cnblogs.com/leonn/archive/2010/04/08/1706833.html

Guess you like

Origin blog.csdn.net/weixin_30470643/article/details/94998956