当前位置:网站首页>Bubble sort, select sort, direct insert sort
Bubble sort, select sort, direct insert sort
2022-06-22 20:01:00 【Just one word】
1. Bubble sort
Existing array a[10], Every time we loop from the element a[9] Start , take a[j] And a[j-1] The comparison , If a[j] Small , Will a[j] Move forward , Continue to compare with the previous values in pairs , Will move the minimum value to the front of the array .
Outer loop n-1 Time (n Is array length ), Each inner loop starts at the tail , use a[j] And a[j-1] Compare two by two , Small value keeps moving forward .
Time complexity O(n**2)
Code :
#include <stdio.h>
void BubbleSort(int k[], int n)
{
int i, j, temp, count1=0, count2=0;
for( i=0; i < n-1; i++ )
{
for( j=n-1; j > i; j-- )
{
count1++;
if( k[j-1] > k[j] )
{
count2++;
temp = k[j-1];
k[j-1] = k[j];
k[j] = temp;
}
}
}
printf(" All in all %d Compare it to , the %d Secondary mobility !", count1, count2);
}
int main()
{
int i;
int a[10] = {
5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
BubbleSort(a, 10);
printf(" The result of sorting is :");
for( i=0; i < 10; i++ )
{
printf("%d", a[i]);
}
printf("\n\n");
return 0;
}
2. Selection sort
Outer loop n-1 Time , Every time a[i] As a minimum , During internal circulation, it will start from... Every time [i:n-1] Minimum value found in the element of , Record the minimum subscript first , After an inner loop is executed , Then add the minimum value and a[i] Swap places .
Time complexity :O(n**2), But it will move less than bubbling
Code :
#include <stdio.h>
void SelectSort(int k[], int n)
{
int i, j, min, temp, count1=0, count2=0;
for( i=0; i < n-1; i++ )
{
min = i;
for( j=i+1; j < n; j++ )
{
count1++;
if( k[j] < k[min] )
{
min = j;
}
}
if( min != i )
{
count2++;
temp = k[min];
k[min] = k[i];
k[i] = temp;
}
}
printf(" All in all %d Compare it to , the %d Secondary mobility !", count1, count2);
}
int main()
{
int i, a[10] = {
5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
SelectSort(a, 10);
printf(" The result of sorting is :");
for( i=0; i < 10; i++ )
{
printf("%d", a[i]);
}
printf("\n\n");
return 0;
}
3. Direct insert sort
Divide the array into ordered and unordered areas , The initial phase assumes that the first element is already ordered , Insert the first element in the unordered area into the ordered area according to the size each time .
Time complexity :O(n**2)
#include <stdio.h>
void InsertSort(int k[], int n)
{
int i, j, temp;
for( i=1; i < n; i++ )
{
if( k[i] < k[i-1] )
{
temp = k[i];
for( j=i-1; k[j] > temp && j> = 0; j-- )
{
k[j+1] = k[j];
}
k[j+1] = temp;
}
}
}
int main()
{
int i, a[10] = {
5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
InsertSort(a, 10);
printf(" The result of sorting is :");
for( i=0; i < 10; i++ )
{
printf("%d", a[i]);
}
printf("\n\n");
return 0;
}
边栏推荐
猜你喜欢

1.2-----机械设计工具(CAD软件)和硬件设计工具(EDA软件)及对比

【深入理解TcaplusDB技术】入门TcaplusDB 问题汇总

推荐一个解剖学网站

华为云招募工业智能领域合作伙伴,强力扶持+商业变现

图的定义及术语

Traversal of trees and forests

数字经济加速落地,能为中小企业带来什么?

Definitions and terms of drawings

Follow up course supplement of little turtle teacher "take you to learn C and take you to fly"

Comparison of NAND flash particles SLC, MLC, TLC and QLC
随机推荐
[in depth understanding of tcapulusdb technology] tcapulusdb regular documents
Upgrade VS2008 crystal report to the corresponding version of vs2013
0.0 - how can SolidWorks be uninstalled cleanly?
[petty bourgeoisie database] break down the concept: data, database, database system, database management system, database technology
matlab调用API
delegate
MySQL约束
漫话Redis源码之一百二十二
树和森林的遍历
程序员应该怎么查日期
Velocity syntax
0.0 - Solidworks如何才能卸载干净?
Definitions and terms of drawings
康考迪亚大学|图卷积循环网络用于强化学习中的奖励生成
区间检索SQL性能优化方法
[in depth understanding of tcapulusdb technology] how to take tcapulusdb off the shelf
Openpnp debugging ------ 0816 Feida Tui 0402 taping
记可视化项目代码设计的心路历程以及理解
【深入理解TcaplusDB技术】TcaplusDB机型
Install some office tools