几个排序算法的实现
近日,想复习下数据结构,选择了几个算法进行实现。不妥之处,请提出。写的比较乱。
公有方法的实现:主要是2个数据的交换,这个有很多实现方法。当然,你也可以不用临时变量进行交换。
2。插入排序
3。选择排序
4。快速排序
public class QuickSort:AbstractSort {
private int Partition(int[] arr, int low, int high) {
int pivot = arr[low];
while (low < high) {
while (low < high && arr[high] >= pivot)
--high;
ExChange.InterChange(ref arr[high], ref arr[low]);
while (low < high && arr[low] <= pivot)
++low;
ExChange.InterChange(ref arr[high], ref arr[low]);
}
arr[low] = pivot;
return low;
}
![]()
private void Sort(int[] array, int low, int high) {
if (low < high) {
int partition = Partition(array, low, high);
Sort(array, low, partition - 1);
Sort(array, partition + 1, high);
}
}
public override void Sort(int[] sourceArray) {
if (null == sourceArray || sourceArray.Length == 1) return;
Sort(sourceArray, 0, sourceArray.Length - 1);
}
}
公有方法的实现:主要是2个数据的交换,这个有很多实现方法。当然,你也可以不用临时变量进行交换。
1
public class ExChange {
2
public static void InterChange(ref Int32 num1, ref Int32 num2) {
3
int temp = num1;
4
num1 = num2;
5
num2 = temp;
6
}
7
}
基类的实现:这么多排序,先稍微建立个抽象类吧,类里只有一个抽象方法Sort
public class ExChange {2
public static void InterChange(ref Int32 num1, ref Int32 num2) {3
int temp = num1;4
num1 = num2;5
num2 = temp;6
}7
}1
public abstract class AbstractSort {
2
public abstract void Sort(int[] sourceArray);
3
}
1。冒泡排序
public abstract class AbstractSort {2
public abstract void Sort(int[] sourceArray);3
} 1
public override void Sort(int[] sourceArray) {
2
if (null == sourceArray || sourceArray.Length == 1) return;
3
for (int i = 0; i < sourceArray.Length; i++) {
4
for (int j = 0; j < sourceArray.Length - i - 1; j++) {
5
if (sourceArray[j] < sourceArray[j + 1]) {
6
ExChange.InterChange(ref sourceArray[j], ref sourceArray[j + 1]);
7
}
8
}
9
}
10
}
插一句,算法贵在掌握和应用,其实我做的并不好。比如说到冒泡涉及到两两交换,那么设计一个扑克牌打乱的算法,你会怎么设计呢?仔细想想,用两个Random函数,进行两两交换不就行了吗?
public override void Sort(int[] sourceArray) {2
if (null == sourceArray || sourceArray.Length == 1) return;3
for (int i = 0; i < sourceArray.Length; i++) {4
for (int j = 0; j < sourceArray.Length - i - 1; j++) {5
if (sourceArray[j] < sourceArray[j + 1]) {6
ExChange.InterChange(ref sourceArray[j], ref sourceArray[j + 1]);7
}8
}9
}10
}2。插入排序
1
public class InsertSort:AbstractSort {
2
public override void Sort(int[] sourceArray) {
3
if (null == sourceArray || sourceArray.Length == 1) return;
4
for (int i = 1; i < sourceArray.Length; i++) {
5
int temp = sourceArray[i];
6
for (int j = i - 1; j > -1; j--) {
7
if (temp < sourceArray[j]) {
8
sourceArray[j + 1] = sourceArray[j];
9
sourceArray[j] = temp;
10
}
11
}
12
}
13
}
14
}
public class InsertSort:AbstractSort {2
public override void Sort(int[] sourceArray) {3
if (null == sourceArray || sourceArray.Length == 1) return;4
for (int i = 1; i < sourceArray.Length; i++) {5
int temp = sourceArray[i];6
for (int j = i - 1; j > -1; j--) {7
if (temp < sourceArray[j]) {8
sourceArray[j + 1] = sourceArray[j];9
sourceArray[j] = temp;10
}11
}12
}13
}14
}3。选择排序
1
public class SelectSort : AbstractSort{
2
public override void Sort(int[] sourceArray) {
3
if (null == sourceArray || sourceArray.Length == 1) return;
4
for (int i = 0; i < sourceArray.Length; i++) {
5
int changeIndex = 0;
6
bool needChange = false;
7
int small = sourceArray[i];
8
for (int j = i + 1; j < sourceArray.Length; j++) {
9
if (sourceArray[j] < small) {
10
changeIndex = j;
11
needChange = true;
12
small = sourceArray[j];
13
}
14
}
15
if (needChange) ExChange.InterChange(ref sourceArray[changeIndex], ref sourceArray[i]);
16
}
17
}
18
}
public class SelectSort : AbstractSort{2
public override void Sort(int[] sourceArray) {3
if (null == sourceArray || sourceArray.Length == 1) return;4
for (int i = 0; i < sourceArray.Length; i++) {5
int changeIndex = 0;6
bool needChange = false;7
int small = sourceArray[i];8
for (int j = i + 1; j < sourceArray.Length; j++) {9
if (sourceArray[j] < small) {10
changeIndex = j;11
needChange = true;12
small = sourceArray[j];13
}14
}15
if (needChange) ExChange.InterChange(ref sourceArray[changeIndex], ref sourceArray[i]);16
}17
}18
}4。快速排序
public class QuickSort:AbstractSort {
private int Partition(int[] arr, int low, int high) {
int pivot = arr[low];
while (low < high) {
while (low < high && arr[high] >= pivot)
--high;
ExChange.InterChange(ref arr[high], ref arr[low]);
while (low < high && arr[low] <= pivot)
++low;
ExChange.InterChange(ref arr[high], ref arr[low]);
}
arr[low] = pivot;
return low;
}
private void Sort(int[] array, int low, int high) {
if (low < high) {
int partition = Partition(array, low, high);
Sort(array, low, partition - 1);
Sort(array, partition + 1, high);
}
}
public override void Sort(int[] sourceArray) {
if (null == sourceArray || sourceArray.Length == 1) return;
Sort(sourceArray, 0, sourceArray.Length - 1);
}
}



浙公网安备 33010602011771号