之前寫了一個C#版冒泡排序的實現。由于之前的版本是用兩層for循環來實現的,這個版本的排序還是有進一步優化的空間的。
之前的排序:
int temp = 0;
for (int i = arr.Length - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
能夠看出,由于有兩層for循環的存在,使得整個排序必須要進行(n - 2)!次判斷,即使第一次產生的數組就是已經排好序的。
對這個版本的冒泡排序進行優化:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _0301數組的冒泡排序_優化
{
class Program
{
static void Main(string[] args)
{
int len = 0; //數組大小
int min = 0; //數組下界
int max = 0; //數組上界
Console.WriteLine("下面將產生一個隨機數組");
//接收數組大小
do
{
Console.WriteLine("請輸入數組大小,它必須是一個小于等于10的正整數:");
len = int.Parse(Console.ReadLine());
} while ((len <= 0) || (len > 10));
//接收數組下界
Console.WriteLine("請輸入數組下界,它必須是一個整數:");
min = int.Parse(Console.ReadLine());
//接收數組上界
do
{
Console.WriteLine("請輸入數組上界,它必須是一個整數,并且比數組小值大,且能讓數組容納{0}個數:", len);
max = int.Parse(Console.ReadLine());
} while ((max <= min) || ((max - min + 1) < len));
Console.WriteLine();
Console.WriteLine("數組長度:{0}\n數組下界:{1}\n數組上界:{2}", len, min, max);
Console.WriteLine("生成數組如下:");
int iSeed = 6;
Random ra = new Random(iSeed);
int[] arr = new int[len];
//打印原數組的值
for (int i = 0; i < arr.Length; i++)
{
arr[i] = ra.Next(min, max);
Console.Write(arr[i]);
if (i < arr.Length - 1)
{
Console.Write(",");
}
else
{
Console.WriteLine();
}
}
//開始進行數組排序
Console.WriteLine("數組產生完畢,開始排序");
Console.WriteLine();
#region 原有排序,這種排序必須要比較(n -2)!次
//int temp = 0;
//for (int i = arr.Length - 1; i > 0; i--)
//{
// for (int j = 0; j < i; j++)
// {
// if (arr[j] > arr[j + 1])
// {
// temp = arr[j + 1];
// arr[j + 1] = arr[j];
// arr[j] = temp;
// }
// }
//}
#endregion
//這種排序多比較(n - 2)!次,但如果當前已是優排序結果則直接停止比較
int temp = 0;
bool swapped = true;
do
{
swapped = false;
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] > arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
//打印排序后的結果
Console.WriteLine("排序后結果:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i]);
if (i < arr.Length - 1)
{
Console.Write(",");
}
else
{
Console.WriteLine();
}
}
Console.WriteLine("程序結束");
Console.ReadKey();
}
}
}
這個版本的排序將外層循環改成了do...while()實現,并設置一個標識變量swapped,如果本次內部循環未進行一次數據置換則說明數組序列已實現優,立刻結束外層循環。這種排序多比較(n - 2)!次,但如果當前已是優排序結果則直接停止比較。
其實外層循環是for循環也可以實現這種效果,但相對來說do...while()寫起來更優雅一些。
之前的排序:
int temp = 0;
for (int i = arr.Length - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
能夠看出,由于有兩層for循環的存在,使得整個排序必須要進行(n - 2)!次判斷,即使第一次產生的數組就是已經排好序的。
對這個版本的冒泡排序進行優化:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _0301數組的冒泡排序_優化
{
class Program
{
static void Main(string[] args)
{
int len = 0; //數組大小
int min = 0; //數組下界
int max = 0; //數組上界
Console.WriteLine("下面將產生一個隨機數組");
//接收數組大小
do
{
Console.WriteLine("請輸入數組大小,它必須是一個小于等于10的正整數:");
len = int.Parse(Console.ReadLine());
} while ((len <= 0) || (len > 10));
//接收數組下界
Console.WriteLine("請輸入數組下界,它必須是一個整數:");
min = int.Parse(Console.ReadLine());
//接收數組上界
do
{
Console.WriteLine("請輸入數組上界,它必須是一個整數,并且比數組小值大,且能讓數組容納{0}個數:", len);
max = int.Parse(Console.ReadLine());
} while ((max <= min) || ((max - min + 1) < len));
Console.WriteLine();
Console.WriteLine("數組長度:{0}\n數組下界:{1}\n數組上界:{2}", len, min, max);
Console.WriteLine("生成數組如下:");
int iSeed = 6;
Random ra = new Random(iSeed);
int[] arr = new int[len];
//打印原數組的值
for (int i = 0; i < arr.Length; i++)
{
arr[i] = ra.Next(min, max);
Console.Write(arr[i]);
if (i < arr.Length - 1)
{
Console.Write(",");
}
else
{
Console.WriteLine();
}
}
//開始進行數組排序
Console.WriteLine("數組產生完畢,開始排序");
Console.WriteLine();
#region 原有排序,這種排序必須要比較(n -2)!次
//int temp = 0;
//for (int i = arr.Length - 1; i > 0; i--)
//{
// for (int j = 0; j < i; j++)
// {
// if (arr[j] > arr[j + 1])
// {
// temp = arr[j + 1];
// arr[j + 1] = arr[j];
// arr[j] = temp;
// }
// }
//}
#endregion
//這種排序多比較(n - 2)!次,但如果當前已是優排序結果則直接停止比較
int temp = 0;
bool swapped = true;
do
{
swapped = false;
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] > arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
//打印排序后的結果
Console.WriteLine("排序后結果:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i]);
if (i < arr.Length - 1)
{
Console.Write(",");
}
else
{
Console.WriteLine();
}
}
Console.WriteLine("程序結束");
Console.ReadKey();
}
}
}
這個版本的排序將外層循環改成了do...while()實現,并設置一個標識變量swapped,如果本次內部循環未進行一次數據置換則說明數組序列已實現優,立刻結束外層循環。這種排序多比較(n - 2)!次,但如果當前已是優排序結果則直接停止比較。
其實外層循環是for循環也可以實現這種效果,但相對來說do...while()寫起來更優雅一些。