以前对排序算法没有一个很好的认知,前一段时间在51NOD上刷题的时候,被大量数据的排序给困住了,好几种都是超时的,这也使我多学习了几种常用的排序算法,最后通过快排解决了问题,下边是当时提交的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace QuickSort
{
//1018 排序
//---------------------------------------------------------------
//基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
//给出N个整数,对着N个整数进行排序
//---------------------------------------------------------------
//Input
//第1行:整数的数量N(1 <= N <= 50000)
//第2 - N + 1行:待排序的整数(-10^9 <= A[i] <= 10^9)
//Output
//共n行,按照递增序输出排序好的数据。
//---------------------------------------------------------------
//Input示例
//5
//5
//4
//3
//2
//1
//Output示例
//1
//2
//3
//4
//5
class Program
{
static void Main(string[] args)
{
#region 读取txt,50000条数据
// int[] numList = new int[50000];
// StreamReader sr = new StreamReader(@"C:\Users\yuanj\Desktop\test.txt", Encoding.Default);
// string line;
// int n = 0;
// while ((line = sr.ReadLine()) != null) {
// numList[n] = int.Parse(line.ToString());
// n++;
// }
#endregion
#region 手动输入数据
int n = int.Parse(Console.ReadLine());
int[] numList = new int[n];
for (int i = 0; i < n; i++)
{
numList[i] = int.Parse(Console.ReadLine());
}
#endregion
Quicksort(numList, 0, numList.Length - 1);
for (int i = 0; i < numList.Length; i++)
{
Console.WriteLine(numList[i]);
}
Console.ReadLine();
}
#region 冒泡排序------当数据量达到50000的时候,会超时
// 分类 -------------- 内部比较排序
// 数据结构 ---------- 数组
// 最差时间复杂度 ---- O(n^2)
// 最优时间复杂度 ---- 如果序列在一开始已经大部分排序过的话,会接近O(n)
// 平均时间复杂度 ---- O(n^2)
// 所需辅助空间 ------ O(1)
// 稳定性 ------------ 稳定
// 冒泡排序进阶,鸡尾酒排序
//static void Sort(int[] numList)
//{
// bool flag = true;
// for (int i = 0; i < numList.Length - 1; i++)
// {
// for (int j = 0; j < numList.Length - 1 - i; j++)
// {
// if (numList[j + 1] < numList[j])
// {
// flag = false;
// int temp = numList[j + 1];
// numList[j + 1] = numList[j];
// numList[j] = temp;
// }
// }
// if (flag)
// {
// break;
// }
// }
//}
#endregion
#region 选择排序---和冒泡排序一样的超时数据
// 分类 -------------- 内部比较排序
// 数据结构 ---------- 数组
// 最差时间复杂度 ---- O(n^2)
// 最优时间复杂度 ---- O(n^2)
// 平均时间复杂度 ---- O(n^2)
// 所需辅助空间 ------ O(1)
// 稳定性 ------------ 不稳定
//static void Sort(int[] numList) {
// int temp;
// for (int i = 0; i < numList.Length-1; i++)
// {
// int min = i;
// for (int j = i+1; j < numList.Length; j++)
// {
// if (numList[j] < numList[min]) {
// min = j;
// }
// }
// temp = numList[i];
// numList[i] = numList[min];
// numList[min] = temp;
// }
//}
#endregion
#region 希尔排序--插入排序的一种
// 分类 -------------- 内部比较排序
// 数据结构 ---------- 数组
// 最差时间复杂度 ---- 根据步长序列的不同而不同。已知最好的为O(n(logn)^2)
// 最优时间复杂度 ---- O(n)
// 平均时间复杂度 ---- 根据步长序列的不同而不同。
// 所需辅助空间 ------ O(1)
// 稳定性 ------------ 不稳定
// 51nod上最差复杂度会出现,20组数据有一组超时,冒泡和选择两种为三组超时
//static void Sort(int[] list)
//{
// int inc;
// for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
// for (; inc > 0; inc /= 3)
// {
// for (int i = inc + 1; i <= list.Length; i += inc)
// {
// int t = list[i - 1];
// int j = i;
// while ((j > inc) && (list[j - inc - 1] > t))
// {
// list[j - 1] = list[j - inc - 1];
// j -= inc;
// }
// list[j - 1] = t;
// }
// }
//}
#endregion
//快速排序
public static void Quicksort(int[] a, int low, int high)
{
if (low >= high)
{
return;
}
int first = low, last = high;
//此时a[low]被保存到key,所以元素a[low]可以当作是一个空位,用于保存数据,之后每赋值一次,也会有一个位置空出来,直到last==first,此时a[last]==a[first]=key
int key = a[low];
while (first < last)
{
while (first < last && a[last] >= key)
{
last--;
}
a[first] = a[last];
while (first < last && a[first] <= key)
{
first++;
}
a[last] = a[first];
}
a[first] = key;
//递归排序数组左边的元素
Quicksort(a, low, first - 1);
//递归排序右边的元素
Quicksort(a, first + 1, high);
}
//static void Sort(int[] numList,int left,int right)
//{
// if (left < right) {
// int x = numList[left];
// int i = left;
// int j = right;
// while (i < j) {
// while (i < j) {
// if (numList[i] <= x)
// {
// numList[i] = numList[j];
// break;
// }
// else {
// j--;
// }
// }
// while (i < j) {
// if (numList[i] > x)
// {
// numList[j] = numList[i];
// break;
// }
// else {
// i++;
// }
// }
// }
// numList[i] = x;
// Sort(numList, left, i - 1);
// Sort(numList, i + 1, right);
// }
//}
}
}