#include<iostream>
using namespace std;
const int MaxSize = 20;
typedef int KeyType;
typedef struct {//顺序表的结构
KeyType key;
int data;
}RecType;
typedef struct {//顺序表
RecType data[MaxSize];
int length;
}Sqlist;
void CreateList(Sqlist*& L, int a[], int n) {//建立顺序表
int i = 0, k = 0;
L = (Sqlist*)malloc(sizeof(Sqlist));
while (i < n)
{
L->data[k].key = a[i];
k++, i++;
}
L->length = k;
}
void BubbleSort(RecType R[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = n - 1; j > i; j--)
if (R[j].key < R[j - 1].key)
swap(R[j], R[j - 1]);
}
void DestroyList(Sqlist*& L) {//销毁顺序表
free(L);
}
int main()
{
int a[] = { 12,16,4,7,8,19,10,20,12,9 };
int n = 10;
Sqlist* b;
CreateList(b, a, n);
BubbleSort(b->data, n);
for (int i = 0; i < n; i++) {
cout << b->data[i].key;
cout << " ";
}
DestroyList(b);
return 0;
}
顺序表的冒泡排序
最新推荐文章于 2023-03-06 21:24:59 发布