C ++程序进行St回排序
StoogeSort用于对给定数据进行排序。它是一种递归排序算法。StoogeSort将数组分为两个重叠的部分,每个部分为2/3,并通过对I,II和I进行排序,分三步对数组进行排序。该算法的最坏情况下时间复杂度为O(n^2.7095)。
算法
Begin Take input of data. Call StoogeSort() function with ‘a’ the array of data and ‘n’ the number of values, in the argument list. Implement Sorting using recursive approach. Divide the array into first 2/3 element as part I and last 2/3 as part II. Then send the first, second and again first part into StoogeSort(). If the length is not further breakable then swap element at the start and end if a[end] < a[start]. Return to main and display the result. End.
范例程式码
#include<iostream>
using namespace std;
void StoogeSort(int a[],int start, int end) {
int temp;
if(end-start+1 > 2) {
temp = (end-start+1)/3;
StoogeSort(a, start, end-temp);
StoogeSort(a, start+temp, end);
StoogeSort(a, start, end-temp);
}
if(a[end] < a[start]) {
temp = a[start];
a[start] = a[end];
a[end] = temp;
}
}
int main() {
int m, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>m;
int arr[m];
for(i = 0; i < m; i++) {
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
StoogeSort(arr, 0, m-1);
cout<<"\nSorted Data ";
for (i = 0; i < m; i++)
cout<<"->"<<arr[i];
return 0;
}输出结果
Enter the number of data element to be sorted: 4 Enter element 1: 6 Enter element 2: 7 Enter element 3: 3 Enter element 4: 2 Sorted Data ->2->3->6->7
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短