总结
1.视频资源P59-P66:
https://www.bilibili.com/video/BV1gE41157pC?p=8&spm_id_from=pageDriver
2.学习内容:
1)冒泡排序 P59;
2)泛型冒泡排序 P60;
3)选择排序 P61;
4)排序算法测试辅助工具 P62;
5)插入排序 P63;
6)归并排序的归并过程 P64;
7)归并排序及递归调用 P65;
8)归并排序优化 P66;
9)快速排序 P67;
10)随机化快速排序 P68;
11)三向切分的随机化快速排序 P69。
2021/11/07、08、09、10、11回溯
紫色笔为回溯后增加
1)根据伪代码编写冒泡排序算法类;
2)根据伪代码编写选择、插入(自己也写了一个方法)排序算法类;
3)根据伪代码编写归并排序版本1类;
4)根据伪代码编写归并排序版本优化类(递归调用部分);
5)根据伪代码编写快速排序类。
具体内容
P59 冒泡排序
1、冒泡排序思想;
2、新建项目;
3、新建冒泡排序类。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class BubbleSort
{
public static void Sort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
Swap(arr, j, j + 1);
}
}
}
}
public static void Swap(int[] arr ,int i,int j)
{
int c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
}
P60 泛型冒泡排序
1、新建泛型冒泡排序类;
2、自定义数据类型排序。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class BubbleSortGeneric
{
public static void Sort<E>(E[] arr) where E:IComparable<E>
{
int n = arr.Length;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (arr[j].CompareTo( arr[j + 1])>0)
{
Swap(arr, j, j + 1);
}
}
}
}
public static void Swap<E>(E[] arr, int i, int j)
{
E c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace Sorting
{
class Data:IComparable<Data>
{
private int year;
private int month;
private int day;
private string happen;
public Data(int year, int month,int day, string happen)
{
this.year = year;
this.month = month;
this.day = day;
this.happen = happen;
}
public int CompareTo([AllowNull] Data other)
{
//返回值 < 0 放在前面
//返回值 > 0 放在后面
//返回值 = 0 按顺序
if (this.year >other.year)
{
return 1;
}
if (this.year < other.year)
{
return -1;
}
if (this.month > other.month)
{
return 1;
}
if (this.month < other.month)
{
return -1;
}
if (this.day > other.day)
{
return 1;
}
if (this.day < other.day)
{
return -1;
}
return 0;
}
public override string ToString()
{
return year + "/" + month + "/" + day + ":" + happen;
}
}
}
P61 选择排序
1、选择排序思想;
2、新建选择排序类。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class SelectSort
{
public static void Sort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n; i++)
{
int min = i;
for (int j = i+1; j < n; j++)
{
if (arr[j] < arr[min])
{
min = j;
}
}
Swap(arr, i, min);
}
}
public static void Swap(int[] arr, int i, int j)
{
int c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
}
P62 排序算法测试辅助工具
P63 插入排序
1、插入排序思想;
2、新建插入排序类;
3、比较选择排序与插入排序性能
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class InsertSort
{
public static void Sort(int[] arr)
{
int n = arr.Length;
//方法一:
for (int i = 1; i < n; i++)
{
for (int j = i; j > 0; j--)
{
if (arr[j] < arr[j - 1])
{
Swap(arr, j, j - 1);
}
}
}
//方法二:
//for (int i = 1; i < n; i++)
//{
// int e = arr[i];
// for (int j = i; j > 0; j--)
// {
// if (e < arr[j - 1])
// {
// arr[i] = arr[j - 1];
// //Swap(arr, j, j - 1);
// }
// else
// {
// break;
// }
// }
//}
}
public static void Swap(int[] arr, int i, int j)
{
int c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
}
P64 归并排序的归并过程
1、归并排序思想;
2、新建归并排序类。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class MergeSort1
{
public static void Sort(int[] arr)
{
int n = arr.Length;
int[] temp =new int[n];
Sort(arr, temp, 0, n-1) ;
}
private static void Sort(int[] arr, int[] temp, int l, int r)
{
int mid = ((r - l) + l) / 2;
mergeSort(arr, temp, l, mid, r);
}
private static void mergeSort(int[] arr, int[] temp, int l, int mid, int r)
{
int i = l;
int j = mid + 1;
int k = l;
//左右半边均有元素
while (i <= mid && j <= r)
{
if (arr[i] < arr[j])
{
temp[k] = arr[i];
k++;
i++;
}
else
{
temp[k] = arr[j];
k++;
j++;
}
}
//左半边有元素,右半边无元素
while (i <= mid && j > r)
{
temp[k] = arr[i];
k++;
i++;
}
//左半边无元素,右半边有元素
while (i > mid && j <= r)
{
temp[k] = arr[j];
k++;
j++;
}
//将temp数组放进arr数组中
for (int z = l; z <= r; z++)
{
arr[z] = temp[z];
}
}
}
}
P65 归并排序及递归调用
1、保证左右有序;
2、递归分割数组;
3、优化P64归并排序类;
4、对递归过程进行讲解;
5、归并排序性能分析;
6、比较归并排序与插入排序性能。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class MergeSort1Pluse
{
public static void Sort(int[] arr)
{
int n = arr.Length;
int[] temp =new int[n];
Sort(arr, temp, 0, n-1) ;
}
private static void Sort(int[] arr, int[] temp, int l, int r)
{
if (r<=l)
{
return;
}
int mid = l + (r - l) / 2;
Sort(arr, temp, l, mid);
Sort(arr, temp, mid+1, r);
mergeSort(arr, temp, l, mid, r);
}
private static void mergeSort(int[] arr, int[] temp, int l, int mid, int r)
{
int i = l;
int j = mid + 1;
int k = l;
//左右半边均有元素
while (i <= mid && j <= r)
{
if (arr[i] < arr[j])
{
temp[k] = arr[i];
k++;
i++;
}
else
{
temp[k] = arr[j];
k++;
j++;
}
}
//左半边有元素,右半边无元素
while (i <= mid && j > r)
{
temp[k] = arr[i];
k++;
i++;
}
//左半边无元素,右半边有元素
while (i > mid && j <= r)
{
temp[k] = arr[j];
k++;
j++;
}
//将temp数组放进arr数组中
for (int z = l; z <= r; z++)
{
arr[z] = temp[z];
}
}
}
}
P66 归并排序优化
1、新建第二版归并排序类;
2、比较优化前后归并排序性能;
3、测试几乎为有序数组时归并排序性能。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class MergeSort2
{
public static void Sort(int[] arr)
{
int n = arr.Length;
int[] temp =new int[n];
Sort(arr, temp, 0, n-1) ;
}
private static void Sort(int[] arr, int[] temp, int l, int r)
{
//优化1:小规模数组使用插入排序,减小递归带来的损耗
if (r - l + 1 < 15)
{
InsertSort.Sort1(arr,l,r);
return;
}
else
{
int mid = l + (r - l) / 2;
Sort(arr, temp, l, mid);
Sort(arr, temp, mid + 1, r);
//优化2:数组有序时使用插入排序
if (arr[mid]>arr[mid+1])
{
mergeSort(arr, temp, l, mid, r);
}
}
}
private static void mergeSort(int[] arr, int[] temp, int l, int mid, int r)
{
int i = l;
int j = mid + 1;
int k = l;
//左右半边均有元素
while (i <= mid && j <= r)
{
if (arr[i] < arr[j])
{
temp[k] = arr[i];
k++;
i++;
}
else
{
temp[k] = arr[j];
k++;
j++;
}
}
//左半边有元素,右半边无元素
while (i <= mid && j > r)
{
temp[k] = arr[i];
k++;
i++;
}
//左半边无元素,右半边有元素
while (i > mid && j <= r)
{
temp[k] = arr[j];
k++;
j++;
}
//将temp数组放进arr数组中
for (int z = l; z <= r; z++)
{
arr[z] = temp[z];
}
}
}
}
P67 快速排序
1、快速排序思想;
2、新建快速排序1类;
3、性能比较:优化归并排序、快速排序1;
4、可读性问题。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class QuickSort1
{
public static void Sort(int[] arr)
{
int n = arr.Length;
quickSort(arr,0, n - 1);
}
private static void quickSort(int[] arr, int l, int r)
{
if (r - l + 1 < 15)
{
InsertSort.Sort1(arr, l, r);
return;
}
int v = arr[l];
int j = l;//arr[l+1...l]<v arr[j+1...i-1]>v
int i = l + 1;
while (i <= r)
{
if (arr[i] >= v)
{
i++;
}
else
{
j++;
Swap(arr, i, j);
}
}
Swap(arr, l, j);
quickSort(arr, l, j);
quickSort(arr, j + 1, r);
}
public static void Swap(int[] arr, int i, int j)
{
int c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
}
P68 随机化快速排序
1、快速排序理想情况;
2、快速排序最不理想情况;
3、新建快速排序2类。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class QuickSort2
{
private static Random rd = new Random();
public static void Sort(int[] arr)
{
int n = arr.Length;
quickSort(arr,0, n - 1);
}
private static void quickSort(int[] arr, int l, int r)
{
if (r - l + 1 < 15)
{
InsertSort.Sort1(arr, l, r);
return;
}
int p = l + rd.Next(r - l + 1);//取不到括号中的值,所以r - l 还要+ 1才对
Swap(arr, l, p);
int v = arr[l];
int j = l;//arr[l+1...l]<v arr[j+1...i-1]>v
int i = l + 1;
while (i <= r)
{
if (arr[i] >= v)
{
i++;
}
else
{
j++;
Swap(arr, i, j);
}
}
Swap(arr, l, j);
quickSort(arr, l, j);
quickSort(arr, j + 1, r);
}
public static void Swap(int[] arr, int i, int j)
{
int c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
}
P69 三向切分的随机化快速排序
1、问题引出;
2、解决方案:三向切分;
3、新建快速排序3类;
4、性能比较:快速排序1、2、3类。
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorting
{
class QuickSort3
{
private static Random rd = new Random();
public static void Sort(int[] arr)
{
int n = arr.Length;
quickSort(arr,0, n - 1);
}
private static void quickSort(int[] arr, int l, int r)
{
if (r - l + 1 < 15)
{
InsertSort.Sort1(arr, l, r);
return;
}
int p = l + rd.Next(r - l + 1);
Swap(arr, l, p);
int v = arr[l];
int lt = l;//arr[l+1...lt] < v
int i = l + 1;//arr[lt+1...i-1] = v
int gt = r + 1; //arr[gt...r] > v
while (i <gt)
{
if (arr[i] > v)
{
gt--;
Swap(arr, i, gt);
}
if (arr[i] < v)
{
lt++;
Swap(arr, i, lt);
i++;
}
if (arr[i] == v)
{
i++;
}
}
Swap(arr, l, lt);
quickSort(arr, l, lt-1);
quickSort(arr, gt, r);
}
public static void Swap(int[] arr, int i, int j)
{
int c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
}