排序算法积累(1)-----冒泡排序,插入排序,选择排序

#include<iostream>
#include<string>
#include<math.h>
#include <cstddef>
#include<stack> //出入栈头文件

using namespace std;


void swap(int arr[], int i, int j);
void Swap(int arr[], int i, int j);
void bubbleSort(int arr[], int size);
void insertionSort(int arr[], int size);
void selectionSort(int arr[], int size);
int* copyArray(int arr[], int size);
bool isEqual(int arr1[], int arr2[]);


int main()
{
    int arraya[5] = { 4, 8, 6, 7, 2 };
    int arrayb[5] = { 4, 8, 6, 7, 2 };
    int arrayc[5] = { 4, 8, 6, 7, 2 };
    int arrayd[5] = { 4, 8, 6, 7, 2 };

    bubbleSort(arraya, 5); //冒泡排序
    for (int i = 0; i<5; i++)
        cout << arraya[i] << ' ';
    cout << endl;

    insertionSort(arrayb, 5); //插入排序
    for (int i = 0; i<5; i++)
        cout << arrayb[i] << ' ';
    cout << endl;

    selectionSort(arrayc, 5);//选择排序
    for (int i = 0; i<5; i++)
        cout << arrayc[i] << ' ';
    cout << endl;

    int *arraytmp; //复制一个数组
    arraytmp = copyArray(arrayd, 5);
    for (int i = 0; i<5; i++)
        cout << arraytmp[i] << ' ';
    cout << endl;

    bool flag;
    flag = isEqual(arraya, arrayb);
    cout << flag << endl;

    return 0;
}
//交换两个数,此方法只能用于整型数据, 
void swap(int arr[], int i, int j)
{
    arr[i] = arr[i] ^ arr[j];
    arr[j] = arr[i] ^ arr[j];
    arr[i] = arr[i] ^ arr[j];
}

//经典的交换两个数的方法,可用于任意数据类型 ,一般用这个 
void Swap(int arr[], int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

//冒泡排序,时间复杂度为O(n2) ,稳定 
void bubbleSort(int arr[], int size) {
    if (arr == NULL || size < 2) {
        return;
    }
    for (int e = size - 1; e > 0; e--) {
        for (int i = 0; i < e; i++) {
            if (arr[i] > arr[i + 1]) {
                Swap(arr, i, i + 1);
            }
        }
    }
}

//插入排序, 时间复杂度为O(n2) ,稳定 
void insertionSort(int arr[], int size) {
    if (arr == NULL || size < 2) {
        return;
    }
    for (int i = 1; i < size; i++) {
        for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
            Swap(arr, j, j + 1);
        }
    }
}

//选择排序 ,时间复杂度为O(n2) ,稳定 
void selectionSort(int arr[], int size) {
    if (arr == NULL || size < 2) {
        return;
    }
    for (int i = 0; i < size; i++) {
        int minIndex = i;
        for (int j = i + 1; j < size; j++) {
            minIndex = arr[j] < arr[minIndex] ? j : minIndex;
        }
        Swap(arr, i, minIndex);
    }
}

//复制一个数组 
int* copyArray(int arr[], int size) {
    if (arr == NULL) {
        return NULL;
    }
    int *res;
    res = new int[size];
    for (int i = 0; i < size; i++) {
        res[i] = arr[i];
    }
    return res;
}

//判断两个数组是否相等 
bool isEqual(int arr1[], int arr2[]) {
    if ((arr1 == NULL && arr2 != NULL) || (arr1 != NULL && arr2 == NULL)) {
        return false;
    }
    if (arr1 == NULL && arr2 == NULL) {
        return true;
    }
    if (sizeof(arr1) / sizeof(arr1[0]) != sizeof(arr2) / sizeof(arr2[0])) {
        return false;
    }
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
        if (arr1[i] != arr2[i]) {
            return false;
        }
    }
    return true;
}

 

posted @ 2017-11-17 15:39  P_langen  阅读(295)  评论(0编辑  收藏  举报