数据结构各种排序算法C++实现
包含了直接插入排序,折半插入排序,希尔排序,冒泡排序,快速排序,选择排序,归并排序,这几种排序的算法实现。堆排序和基数排序没写,觉的代码实现起来有点难,明白了思路但是没写。哈哈哈! 代码:
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct{
int p;
} Elem;
typedef struct{
Elem *elem;
int length;
}Sqlist;
int InitList_Sq(Sqlist &L)
{
float c = 0.0; int i;
L.elem = new Elem[MAXSIZE+1];
if (!L.elem)
exit(-1);
else
L.length = 1;
cout << "顺序表创建成功!请添加新建数据!按0结束!";
for (i = 1; i < MAXSIZE; i++)
{
if (L.elem[i - 1].p == c)
{
break;
}
else
cin >> L.elem[i].p;
}
L.length = i-1;
return 1;
}
int GetLength(Sqlist &L){
return L.length;
}
void PrintList(Sqlist &L){
if (L.length == 0)
{
cout << "此顺序表不存在或无数据元素!";
exit(-2);
}
else
{
for (int i = 1; i < L.length; i++)
{
int e = L.elem[i].p;
cout << e;
}
}
}
void Circle_L(Sqlist &L)
{
cout << "请输入循环左移位数:";
int k;
float b[MAXSIZE];
cin >> k;
k = k%L.length;
for (int i = 0; i < k; i++)
{
b[i] = L.elem[i].p;
}
for (int i = 0; i <= k; i++)
{
L.elem[i].p = L.elem[k + i].p;
}
for (int i = 0; i < k; i++)
{
L.elem[((L.length) - k + i) % L.length].p = b[i];
}
PrintList(L);
}
void DirectOrderInsert(Sqlist &L)
{
int i, j;
for (i = 2; i < L.length; i++)
{
if (L.elem[i].p < L.elem[i - 1].p)
{
L.elem[0] = L.elem[i];
for (j = i - 1; L.elem[0].p < L.elem[j].p; --j)
{
L.elem[j + 1] = L.elem[j];
}
L.elem[j + 1] = L.elem[0];
}
}
}
void BiInserSort(Sqlist &L)
{
for (int i = 2; i < L.length; i++)
{
L.elem[0] = L.elem[i];
int low = 1;
int high = i - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (L.elem[0].p < L.elem[mid].p)
high = mid - 1;
else
low = mid + 1;
}
for (int j = i - 1; j >= high + 1; --j)
{
L.elem[j + 1] = L.elem[j];
}
L.elem[high + 1] = L.elem[0];
}
}
void shell_Insert(Sqlist &L, int dk)
{
int i, j;
for (i = dk+1; i < L.length; i++)
{
if (L.elem[i].p < L.elem[i - dk].p)
{
L.elem[0] = L.elem[i];
for (j = i - dk; j>0 && (L.elem[0].p < L.elem[j].p); j = j - dk)
L.elem[j + dk] = L.elem[j];
L.elem[j+dk] = L.elem[0];
}
}
}
void shellsort(Sqlist &L ,int data[],int t)
{
for (int k = 0; k < t; k++)
{
shell_Insert(L, data[k]);
}
}
void bubble_sort(Sqlist &L)
{
int i, j, k;
int flag = 1;
int n = L.length - 1;
for (i = 1; i <= n-1 && flag==1; i++)
{
flag = 0;
for (j = 1; j <= n - i; j++)
{
if (L.elem[j].p>L.elem[j + 1].p)
{
flag = 1;
k = L.elem[j].p;
L.elem[j].p = L.elem[j + 1].p;
L.elem[j + 1].p = k;
}
}
}
}
int portition(Sqlist &L, int low, int high)
{
L.elem[0] = L.elem[low];
int protkey = L.elem[low].p;
while (low < high)
{
while (low < high && L.elem[high].p >= protkey) --high;
L.elem[low] = L.elem[high];
while (low < high && L.elem[high].p < protkey) ++low;
L.elem[high] = L.elem[low];
}
L.elem[low] = L.elem[0];
return low;
}
void Qsort(Sqlist L, int low, int high)
{
int mid;
if (low < high)
{
mid = portition(L, low, high);
Qsort(L, low, mid - 1);
Qsort(L, mid + 1, high);
}
}
void Select_Sort(Sqlist &L)
{
int i, j,k;
Elem t;
int n = L.length - 1;
for (i = 1; i < n; i++)
{
k = i;
for (j = i+1; j <= n; j++)
{
if (L.elem[j].p < L.elem[k].p)
{
k = j;
}
if (k != i)
{
t = L.elem[i];
L.elem[i] = L.elem[k];
L.elem[k] = t;
}
}
}
}
void merge(Sqlist &L,int low, int mid, int high)
{
Elem place[MAXSIZE];
int i = low, j = mid + 1, k = low;
while (i <= mid && j <= high)
{
if (L.elem[i].p < L.elem[j].p)
{
place[k++] = L.elem[i++];
}
else
{
place[k++] = L.elem[j++];
}
}
while (i <= mid)
{
place[k++] = L.elem[i++];
}
while (j <= high)
{
place[k++] = L.elem[j++];
}
for (int i = low; i <= high; i++)
{
L.elem[i] = place[i];
}
}
void mergesort(Sqlist &L,int a,int b)
{
if (a >= b)
return;
int mid = (a + b) / 2;
mergesort(L,a, mid);
mergesort(L,mid + 1, b);
merge(L,a, mid, b);
}
void Contents()
{
cout << "***************************************\n";
cout << "* 顺 序 表 练 习 *\n";
cout << "* *\n";
cout << "* 1.建立顺序表并录入数据 *\n";
cout << "* 2.查看顺序表数据 *\n";
cout << "* 3.退出程序 *\n";
cout << "* 4.获取顺序表长度 *\n";
cout << "* 5.循环左移 *\n";
cout << "* 6.直接顺序插入排序(插入排序)*\n";
cout << "* 7.折半插入排序(插入排序) *\n";
cout << "* 8.希尔排序(插入排序) *\n";
cout << "* 9.冒泡排序(交换排序) *\n";
cout << "* 10.快速排序(交换排序) *\n";
cout << "* 11.简单选择排序(选择排序) *\n";
cout << "* 12.归并排序 *\n";
cout << "***************************************\n";
}
void main(){
Sqlist L;
Elem e;
int data[3] = { 4, 2, 1 };
int flag, a, n, b;
float k;
flag = 1;
while (flag)
{
system("cls");
Contents();
cin >> a;
switch (a)
{
case 1: if (InitList_Sq(L)){ cout << "数据录取完毕!"; system("PAUSE"); break; }
else{ cout << "失败!"; break; }
case 2: PrintList(L); system("PAUSE"); break;
case 3: exit(-1);
case 4: cout << GetLength(L); system("PAUSE"); break;
case 5: Circle_L(L); system("PAUSE"); break;
case 6: DirectOrderInsert(L); system("PAUSE"); break;
case 7: BiInserSort(L); system("PAUSE"); break;
case 8: shellsort(L,data,3); system("PAUSE"); break;
case 9: bubble_sort(L); system("PAUSE"); break;
case 10: Qsort(L, 1, L.length-1); system("PAUSE"); break;
case 11: Select_Sort(L); system("PAUSE"); break;
case 12: mergesort(L,1,L.length-1); system("PAUSE"); break;
default: printf("输入错误!请重新输入"); system("PAUSE"); break;
}
}
}
不太会调图片的大小,大家凑活看吧!
|