直接插入排序(c++实现)
简单选择排序(c++实现)
冒泡排序(c++实现)
// 基本排序算法.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
void directinsertsorting(int *data, int size) //直接插入排序
{
int temp, j;
for (int i = 1; i < size; i++)
{
if (data[i] < data[i - 1])
{
temp = data[i];
for (j = i - 1; j >= 0 && data[j] > temp; j--)
data[j + 1] = data[j];
data[j + 1] = temp;
}
}
}
void simpleselectsorting(int *data, int size) //简单选择排序
{
int k, j, temp;
for (int i = 0; i < size; i++)
{
k = i;
for (j = i + 1; j < size; j++)
if (data[j] < data[k])
k = j;
if (k != j)
{
temp = data[k];
data[k] = data[i];
data[i] = temp;
}
}
}
void bubblesorting(int *data, int size) //冒泡排序
{
int temp;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (data[i] > data[j])
{
temp = data[j];
data[j] = data[i];
data[i] = temp;
}
}
}
}
void display(int *data, int size)
{
for (int i = 0; i < size; i++)
cout << data[i] << " ";
cout << endl;
}
int main()
{
int data[] = {14,5,10,11,6,1,10};
int size = sizeof(data) / sizeof(data[0]);
//directinsertsorting(data ,size);
//simpleselectsorting(data, size);
//bubblesorting(data, size);
display(data, size);
system("pause");
return 0;
}