// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
class Heap(){
vector<int> heapData;
int heapSize;
};
vector<int> direct_insertion_sort(vector<int> unorder){
for (int i = 1; i <= unorder.size()-1; i++)
{
int key = unorder[i];
int j = i - 1;
while (j>=0 && key<unorder[j])
{
unorder[j + 1] = unorder[j];
j--;
}
unorder[j + 1] = key;
}
return unorder;
}
vector<int> bubble_sort(vector<int> unorder){ // 普通 的冒泡函数
int len = unorder.size();
for (int i = 0; i < len -1; i++)
{
for (int j = len-1; j > i; j--)
{
if (unorder[j] < unorder[j - 1])
swap(unorder[j], unorder[j - 1]); // ******库自带的模板类定义函数***********//
}
}
return unorder;
}
vector<int> improve_bubble_sort(vector<int> unorder){ // 改进后 的冒泡函数 *****最优时间复杂度为O(n)
int len = unorder.size();
for (int i = 0; i < len - 1; i++)
{
bool swapOrNot = false;
for (int j = len - 1; j > i; j--)
{
if (unorder[j] < unorder[j - 1]){
swap(unorder[j], unorder[j - 1]); // ******库自带的模板类定义函数***********//
swapOrNot = true;
}
if (swapOrNot == false)
{
return unorder;
}
}
}
return unorder;
}
vector<int> direct_select_sort(vector<int> unorder){ // 普通 的冒泡函数
int len = unorder.size();
for (int i = 0; i < len - 1; i++)
{
int k = i;
for (int j = i + 1; j < len ; j++)
{
if (unorder[j] < unorder[k])
{
k = j;
}
}
if (k != i)
{
swap(unorder[k], unorder[i]);
}
}
return unorder;
}
void print(vector<int> array){
for (int i = 0; i < array.size(); i++)
{
cout << array[i] << " ";
}
cout << endl;
}
int main(){
int a[7] = { 6, 2, 4, 3, 5, 1, 2 };
vector<int> unorder(a, a + 7);//vector的赋值并不可以像数组一样方便的用花括号方便的完成赋值,这里借用了数组来初始化这个vector,初始化方
//unorder=direct_insertion_sort(unorder);
print(unorder);
unorder = direct_insertion_sort(unorder);
//bubble_sort(unorder);
print(unorder);
system("pause");
return 0;
}