当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Solution de pin hors grille dans altium designer
MySQL数据库DQL查询操作
Redis 大 key 问题
matlab调用API
希尔排序
1.3----- simple setting of 3D slicing software
AB打包有的Shader没有触发IPreprocessShaders的回调
Fault analysis | from data_ Free exception
漫话Redis源码之一百一二十
修改隐含参数造成SQL性能下降案例之一
Openpnp调试 ------ 0816飞达推0402编带
拓扑排序
详解openGauss多线程架构启动过程
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
第六章 操作位和位串(二)
Google | ICML 2022: sparse training status in deep reinforcement learning
编译报错:/usr/bin/ld: /usr/local/lib/libgflags.a(gflags.cc.o): relocation R_X86_64_32S against `.rodata‘
MySQL约束
[deeply understand tcapulusdb technology] cluster management operation
[in depth understanding of tcapulusdb technology] tcapulusdb model








