在起泡排序中,若待排序序列为正序,只需一趟扫描,而待排序序列为反序时,需进行n-1 趟扫描。对于初始序列(94,10,12,18,42,44,67)只需扫描2趟,而对于初始关键字序列(12,18,42,44,67,94,10)就需扫描6趟。造成这种不对称的原因是:每趟扫描仅能使最小数据下沉一个位置,如果改变扫描方向,情况正好相反,即每趟从后往前扫描,都能使当前无序区中最大数据上浮一个位置置。为了改变上述两种情况下的不对称性,可以在排序过程中交替改变扫描方向,称之为双向起泡排序。设计双向起泡排序的算法,并将其时间性能与起泡排序算法的时间性能进行对比。
时间: 2024-04-09 11:30:23 浏览: 21
双向起泡排序算法是对起泡排序算法的改进,通过交替改变扫描方向,可以解决起泡排序中正序和反序时扫描趟数不对称的问题。
下面是双向起泡排序算法的实现:
1. 初始化标志位flag为true,表示正序扫描。
2. 设置左边界left为0,右边界right为待排序序列长度减1。
3. 当flag为true时,从left到right进行正序扫描,比较相邻的元素,如果前者大于后者,则交换它们。
4. 如果进行了交换,则将flag置为false。
5. 将right减1。
6. 当flag为false时,从right到left进行逆序扫描,比较相邻的元素,如果前者大于后者,则交换它们。
7. 如果进行了交换,则将flag置为true。
8. 将left加1。
9. 重复步骤3到步骤8,直到left大于等于right。
双向起泡排序算法相比于起泡排序算法,在正序和反序情况下都能有效地减少扫描趟数。对于正序序列,只需一趟扫描即可完成排序;对于反序序列,也只需n-1趟扫描。因此,双向起泡排序算法的时间性能要优于起泡排序算法。
需要注意的是,双向起泡排序算法的最坏时间复杂度仍然是O(n^2),因此对于大规模数据的排序仍然不是最优选择。在实际应用中,可以考虑使用其他高效的排序算法,如快速排序或归并排序。
相关问题
帮我写一段使用delphi对stringgrid string进行按照字母顺序正反序排序
好的,这个问题属于技术类问题,可以回答。以下是使用 Delphi 对 StringGrid 中的字符串进行正序和反序排序的示例代码:
// 正序排序
procedure SortStringGrid(SG: TStringGrid; ColIndex: Integer);
var
I, J: Integer;
Temp: string;
begin
for I := 1 to SG.RowCount - 1 do
for J := I + 1 to SG.RowCount - 1 do
if CompareStr(SG.Cells[ColIndex, I], SG.Cells[ColIndex, J]) > 0 then
begin
Temp := SG.Rows[J].CommaText;
SG.Rows[J].CommaText := SG.Rows[I].CommaText;
SG.Rows[I].CommaText := Temp;
end;
end;
// 反序排序
procedure SortStringGridReverse(SG: TStringGrid; ColIndex: Integer);
var
I, J: Integer;
Temp: string;
begin
for I := 1 to SG.RowCount - 1 do
for J := I + 1 to SG.RowCount - 1 do
if CompareStr(SG.Cells[ColIndex, I], SG.Cells[ColIndex, J]) < 0 then
begin
Temp := SG.Rows[J].CommaText;
SG.Rows[J].CommaText := SG.Rows[I].CommaText;
SG.Rows[I].CommaText := Temp;
end;
end;
使用方法:
// 正序排序
SortStringGrid(StringGrid1, 1);
// 反序排序
SortStringGridReverse(StringGrid1, 1);
其中,StringGrid1 为要排序的 StringGrid,ColIndex 为要排序的列索引。
在快速排序算法中,为减少最坏情况的出现,可采用“三者取中”规则,每趟划分前,比较A[low]、A[high]和A[(low+high)/2]的大小,取中间值的记录作为轴点。 实现要求: 编程实现快速排序算法(改进实现轴点“三者取中”法的快速排序算法,),改进实现轴点“三者取中”法的快速排序算法(函数命名为quickSortImproved),并实现快速排序算法在各个序列上所需的排序码比较次数的统计,在下列随机序列上进行测试. 1、在如下三组随机序列、反序序列和正序序列上,统计“三者取中”法的快速排序算法在各个序列上所需的排序码比较次数; 2、在如下三组随机序列、反序序列和正序序列上,统计的快速排序算法在各个序列上所需的排序码比较次数。请帮我用c++编程实现
以下是代码实现,其中包括了快速排序和改进实现轴点“三者取中”法的快速排序,并统计了排序码比较次数。
```c++
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
// 快速排序
int quickSort(int a[], int low, int high) {
if (low >= high) {
return 0;
}
int i = low, j = high, pivot = a[low];
while (i < j) {
while (i < j && a[j] >= pivot) {
j--;
}
a[i] = a[j];
while (i < j && a[i] <= pivot) {
i++;
}
a[j] = a[i];
}
a[i] = pivot;
int compareCount = high - low;
compareCount += quickSort(a, low, i - 1);
compareCount += quickSort(a, i + 1, high);
return compareCount;
}
// 改进实现轴点“三者取中”法的快速排序
int quickSortImproved(int a[], int low, int high) {
if (low >= high) {
return 0;
}
if (high - low + 1 >= 3) {
int mid = (low + high) / 2;
if (a[low] > a[mid]) {
swap(a[low], a[mid]);
}
if (a[low] > a[high]) {
swap(a[low], a[high]);
}
if (a[mid] > a[high]) {
swap(a[mid], a[high]);
}
swap(a[mid], a[low]);
}
int i = low, j = high, pivot = a[low];
while (i < j) {
while (i < j && a[j] >= pivot) {
j--;
}
a[i] = a[j];
while (i < j && a[i] <= pivot) {
i++;
}
a[j] = a[i];
}
a[i] = pivot;
int compareCount = high - low;
compareCount += quickSortImproved(a, low, i - 1);
compareCount += quickSortImproved(a, i + 1, high);
return compareCount;
}
int main() {
const int N = 10;
int a1[N], a2[N], a3[N];
srand(time(NULL));
for (int i = 0; i < N; i++) {
a1[i] = rand() % 100;
a2[i] = N - i - 1;
a3[i] = i;
}
cout << "原始序列1: ";
for (int i = 0; i < N; i++) {
cout << a1[i] << " ";
}
cout << endl;
int compareCount = quickSort(a1, 0, N - 1);
cout << "快速排序后的序列1: ";
for (int i = 0; i < N; i++) {
cout << a1[i] << " ";
}
cout << endl;
cout << "快速排序比较次数: " << compareCount << endl << endl;
cout << "原始序列2: ";
for (int i = 0; i < N; i++) {
cout << a2[i] << " ";
}
cout << endl;
compareCount = quickSort(a2, 0, N - 1);
cout << "快速排序后的序列2: ";
for (int i = 0; i < N; i++) {
cout << a2[i] << " ";
}
cout << endl;
cout << "快速排序比较次数: " << compareCount << endl << endl;
cout << "原始序列3: ";
for (int i = 0; i < N; i++) {
cout << a3[i] << " ";
}
cout << endl;
compareCount = quickSort(a3, 0, N - 1);
cout << "快速排序后的序列3: ";
for (int i = 0; i < N; i++) {
cout << a3[i] << " ";
}
cout << endl;
cout << "快速排序比较次数: " << compareCount << endl << endl;
cout << "原始序列1: ";
for (int i = 0; i < N; i++) {
cout << a1[i] << " ";
}
cout << endl;
compareCount = quickSortImproved(a1, 0, N - 1);
cout << "改进实现轴点“三者取中”法的快速排序后的序列1: ";
for (int i = 0; i < N; i++) {
cout << a1[i] << " ";
}
cout << endl;
cout << "改进实现轴点“三者取中”法的快速排序比较次数: " << compareCount << endl << endl;
cout << "原始序列2: ";
for (int i = 0; i < N; i++) {
cout << a2[i] << " ";
}
cout << endl;
compareCount = quickSortImproved(a2, 0, N - 1);
cout << "改进实现轴点“三者取中”法的快速排序后的序列2: ";
for (int i = 0; i < N; i++) {
cout << a2[i] << " ";
}
cout << endl;
cout << "改进实现轴点“三者取中”法的快速排序比较次数: " << compareCount << endl << endl;
cout << "原始序列3: ";
for (int i = 0; i < N; i++) {
cout << a3[i] << " ";
}
cout << endl;
compareCount = quickSortImproved(a3, 0, N - 1);
cout << "改进实现轴点“三者取中”法的快速排序后的序列3: ";
for (int i = 0; i < N; i++) {
cout << a3[i] << " ";
}
cout << endl;
cout << "改进实现轴点“三者取中”法的快速排序比较次数: " << compareCount << endl << endl;
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)