顺序表的增删查改、二分查找、冒泡和快速排序
SeqList 声明文件 #pragma once #define MAX_SIZE 5 typedef int DataType; typedef struct SeqList { DataType array[MAX_SIZE]; size_t size; }SeqList; void PrintSeqList(SeqList* pSeq); void InitSeqList(SeqList* pSeq);//初始化 void PushBack(SeqList* pSeq, DataType x);//尾插 void PopBack(SeqList* pSeq);//尾删 void PushFront(SeqList* pSeq, DataType x);//头插 void PopFront(SeqList* pSeq); void Insert(SeqList* pSeq, size_t pos, DataType x);//插入 int Find(SeqList* pSeq, size_t pos, DataType x);//查找 void Erase(SeqList* pSeq, size_t pos);//删除 int Remove(SeqList* pSeq, DataType x); void RemoveAll(SeqList* pSeq, DataType x);//去重复 void Swap(DataType* left, DataType* right); void BubbleSort(SeqList* pSeq);//冒泡 void SelectSort(SeqList* pSeq);//void SelectSort_OP(SeqList* pSeq)//选择 int BinarySearch(SeqList* pSeq, DataType x);//二分查找 SeqList实现文件 #include"SeqList.h" #include#include #include using namespace std; void InitSeqList(SeqList* pSeq) { memset(pSeq->array, 0, sizeof(DataType)*MAX_SIZE); pSeq->size = 0; } void PushBack(SeqList* pSeq, DataType x) { assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } pSeq->array[pSeq->size] = x; pSeq->size++; } void PopBack(SeqList* pSeq) { assert(pSeq); if (pSeq->size <= 0) { printf("SeqList is Empty\n"); return; } pSeq->size--; } void PushFront(SeqList* pSeq, DataType x) { int begin = pSeq->size-1; assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } for (begin; begin >=0; begin--) { pSeq->array[begin+1] = pSeq->array[begin]; } pSeq->array[0] = x; pSeq->size++; } void PopFront(SeqList* pSeq) { int begin =0; assert(pSeq); if (pSeq->size <= 0) { printf("SeqList is Empty\n"); } for (begin; begin < pSeq->size; begin++) { pSeq->array[begin] = pSeq->array[begin+1]; } pSeq->size--; } void Insert(SeqList* pSeq, size_t pos, DataType x)//插入位置按数组下标顺序 { int begin = pSeq->size; assert(pSeq); assert(pos <= pSeq->size); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } for (begin; begin > pos; begin--) { pSeq->array[begin] = pSeq->array[begin-1]; } pSeq->array[pos] = x; pSeq->size++; } int Find(SeqList* pSeq, size_t pos, DataType x)//查找返回数组下标位置 { int i = pos; assert(pSeq); assert(pos <= pSeq->size); if (pSeq->size <= 0) { printf("SeqList is Empty\n"); } for (i; i < pSeq->size; i++) { if (pSeq->array[i] == x) { return i; } } return -1; } void Erase(SeqList* pSeq, size_t pos) { assert(pSeq); assert(pos <= pSeq->size); if (pSeq->size <= 0) { printf("SeqList is Empty\n"); return; } for (pos; pos < pSeq->size; pos++) { pSeq->array[pos] = pSeq->array[pos +1]; } pSeq->size--; } void PrintSeqList(SeqList* pSeq) { int i = 0; assert(pSeq); for (; i < pSeq->size; i++) { printf("%d ", pSeq->array[i]); } cout << endl; } int Remove(SeqList* pSeq, DataType x)//删除 { int pos; assert(pSeq); pos = Find(pSeq, 0, x); if (pos != -1) { Erase(pSeq, pos); } return pos; } void RemoveAll(SeqList* pSeq, DataType x)//去掉重复 { int count = 0; int begin = 0; assert(pSeq); for (; begin < pSeq->size; begin++) { if (pSeq->array[begin] == x) { count++; } else { pSeq->array[begin - count] = pSeq->array[begin]; } } pSeq ->size -= count; } void Swap(DataType* left, DataType* right) { DataType tmp = *left; *left = *right; *right = tmp; } void BubbleSort(SeqList* pSeq)//冒泡排序 { assert(pSeq); int first = 0; int second = 0; for (first = 0; first < pSeq->size - 1; first++) { //int Flag = 0; for (second = 1; second < pSeq->size - first; second++) { if (pSeq->array[second - 1] > pSeq->array[second]) { Swap(&pSeq->array[second - 1], &pSeq->array[second]); //Flag = 1; } } /*if (Flag == 0) { return; }*/ } } void SelectSort(SeqList* pSeq)//快速排序 { assert(pSeq); int i, j, max; for (j = pSeq->size -1; j >= 0; j--) { max = j; for (i = j-1; i >= 0; i--) { if (pSeq->array[max] < pSeq->array[i]) { max = i; } } Swap(&pSeq->array[max], &pSeq->array[j]); } } void SelectSort_OP(SeqList* pSeq)//快排优化 { assert(pSeq); int i, min, max; int left = 0; int right = pSeq->size - 1; while (left < right) { for (i = left; i <= right; i++) { min = left; max = right; if (pSeq->array[min] > pSeq->array[i]) { Swap(&pSeq->array[left], &pSeq->array[i]); } if (pSeq->array[max] < pSeq->array[i]) { Swap(&pSeq->array[max], &pSeq->array[i]); } } left++; right--; } } int BinarySearch(SeqList* pSeq, DataType x)//二分查找 { int left = 0; int right = pSeq->size - 1; while (left < right) { int mid = left + (right - left) / 2; if (pSeq->array[mid] > x) { right = mid - 1; } else if (pSeq->array[mid] < x) { left = mid + 1; } else { return mid; } } return -1; } 测试文件 int main() { SeqList seqlist; InitSeqList(&seqlist); /*PushBack(&seqlist, 1); PushBack(&seqlist, 2); PushBack(&seqlist, 3); PushBack(&seqlist, 4); PrintSeqList(&seqlist); PopBack(&seqlist); PopBack(&seqlist); PopBack(&seqlist);*/ PushFront(&seqlist, 2); PushFront(&seqlist, 1); PushFront(&seqlist, 4); PushFront(&seqlist, 3); PushFront(&seqlist, 5); PrintSeqList(&seqlist); int key = BinarySearch(&seqlist, 4); printf("%d ", key); cout << key << endl; //BubbleSort(&seqlist); //SelectSort_OP(&seqlist); PrintSeqList(&seqlist); //RemoveAll(&seqlist, 3); /*Erase(&seqlist, 0); PrintSeqList(&seqlist); Erase(&seqlist, 0); Erase(&seqlist, 0); Erase(&seqlist, 0); PrintSeqList(&seqlist*/ //int value = Remove(&seqlist, 4); //printf("%d\n", value); //Insert(&seqlist, 0, 1); //PrintSeqList(&seqlist); /*PopFront(&seqlist); PopFront(&seqlist); PrintSeqList(&seqlist); PopFront(&seqlist); PopFront(&seqlist); PopFront(&seqlist); PopFront(&seqlist);*/ system("pause"); return 0; }
本文名称:顺序表的增删查改、二分查找、冒泡和快速排序
URL链接:http://azwzsj.com/article/ijijcp.html