排序——冒泡,快速,选择,插入

冒泡排序

 1 #include <iostream>
 2 #include<string>
 3 #include<cstring>
 4
 5
 6 using namespace std;
 7 void bubbleSort(int array[],int n)
 8 {
 9     for(int i=0;i<n;i++)
10     {
11         for(int j=0;j<n-i-1;j++)
12         {
13             if(array[j]>array[j+1])
14             {
15                 int temp =array[j];
16                 array[j]=array[j+1];
17                 array[j+1]=temp;
18             }
19         }
20         for(int k=0;k<n;k++) cout<<" "<<array[k];
21         cout<<endl;
22     }
23 }
24 int main()
25 {
26     int n;
27     cin>>n;
28     int a[1000];
29     for(int i=0;i<n;i++) cin>>a[i];
30     bubbleSort(a,n);
31     return 0;
32 }

快速排序

 1 #include <iostream>
 2
 3 using namespace std;
 4
 5 void quickSort(int array[],int left,int right)
 6 {
 7     int ltemp=left,rtemp=right;
 8     int f=array[(left+right)/2];
 9     while(ltemp<rtemp)
10     {
11         while(array[ltemp]<f) ++ltemp;
12         while(array[rtemp]>f) --rtemp;
13         if(ltemp<=rtemp)
14         {
15             int t=array[ltemp];
16             array[ltemp]=array[rtemp];
17             array[rtemp]=t;
18             --rtemp;
19             ++ltemp;
20         }
21     }
22     if(ltemp==rtemp) ltemp++;
23     if(left<rtemp) quickSort(array,left,ltemp-1);  //左半段排序
24     if(ltemp<right) quickSort(array,rtemp+1,right); //右半段排序
25 }
26 int main()
27 {
28     int n;
29     cin>>n;
30     int array[1000];
31     for(int i=0;i<n;i++) cin>>array[i];
32     //int array[] = { 4, 3, 2, 1, 9, 7, 5, 8, 6 };
33     //int size = sizeof(array) / sizeof(*array); //求数组长度
34     quickSort(array, 0, n - 1);
35     for (int i = 0; i < n; i++) cout << array[i] << " ";
36     return 0;
37 }

选择排序

 1 #include <iostream>
 2
 3 using namespace std;
 4
 5 void selectSort(int array[],int n)
 6 {
 7     int index, temp;
 8     for(int i=0;i<n;i++)
 9     {
10         index=i;
11         for(int j=i+1;j<n;j++)
12         {
13             if(array[j]<array[index])
14             {
15                 index=j;
16             }
17         }
18         temp=array[i];
19         array[i]=array[index];
20         array[index]=temp;
21         for(int k=0;k<n;k++) cout<<" "<<array[k];  //输出每一次排序
22         cout<<endl;
23     }
24 }
25
26 int main()
27 {
28     int n;
29     cin>>n;
30     int a[n];
31     for(int i=0;i<n;i++) cin>>a[i];
32     selectSort(a,n);
33     return 0;
34 }

插入排序

 1 #include <iostream>
 2
 3 using namespace std;
 4 void insertSort(int array[],int n)
 5 {
 6     for(int i=1;i<n;i++)
 7     {
 8         for(int j=i;j>0;j--)
 9         {
10             if(array[j]<array[j-1])
11             {
12                 int temp=array[j];
13                 array[j]=array[j-1];
14                 array[j-1]=temp;
15             }
16         }
17         for(int k=0;k<n;k++) cout<<array[k]<<" ";
18         cout<<endl;
19     }
20 }
21 int main()
22 {
23     int n;
24     cin>>n;
25     int array[n];
26     for(int i=0;i<n;i++) cin>>array[i];
27     insertSort(array,n);
28     return 0;
29 }

原文地址:https://www.cnblogs.com/lijuanhu321/p/9691452.html

时间: 2024-08-30 18:26:02

排序——冒泡,快速,选择,插入的相关文章

php 常用四种排序 冒泡,选择,插入,快排

---恢复内容开始--- 1冒泡排序.  [为描述方便,例子全面为升序排列] 简述:假设数组有10个数字,从左向右.依次比较,如果前者大于后者,则两两交换.每一轮将冒泡一个最大数出来,依次循环,完成排序 流程描述:-- 第一次  a[0] 与 a[1]  比如果 a[0] > a[1]  则 a[0] 与 a[1] 交换,然后 a[1] 与 a[2] 交换,依次到  a[8] 与 a[9]  交换.  此轮过后  a[9] 必为  a[0-9]  中的最大值. 第二次(此时 a[9]以是最大值)

第六章 排序-冒泡,选择,插入排序

哔哩哔哩数据结构讲解地址:https://space.bilibili.com/356198029 本代码视频讲解地址:https://www.bilibili.com/video/av74560022 冒泡排序 #include<iostream> using namespace std; void bubble(int[], int); int main() { int array [] = {55,2,6,4,32,12,9,73,26,37}; int len = sizeof(arr

java面试准备之基础排序——冒泡与选择排序

选择排序: public void select(int[] arr){ for(int i=0;i<arr.length;i++){ for(int j=i+1;j<arr.length;j++){ if(arr[j]>arr[i]){ int one = arr[i]; arr[i]=arr[j]; arr[j]=one; } } } } 冒泡排序: public static int[] popo(int[] arr){ for(int i=0;i<arr.length-1;

JAVA排序(冒泡,直接选择,反转)

int[] arr = {45, 34, 53, 43}; 一,直接选择排序 System.out.println(Arrays.toString(arr)); for(int i=0;i<arr.length;i++) { int tem = i; for(int j=i;j<arr.length;j++) { if(arr[j] < arr[tem]) { tem = j; } } int temp1 = arr[i]; arr[i] = arr[tem]; arr[tem] = t

常见排序算法(冒泡、选择、插入、快速、归并C++实现)

常见排序算法(冒泡.选择.插入.快速.归并C++实现) #include <iostream> using namespace std; // 冒泡排序 void bubbleSort (int data[], size_t size) { for (size_t i = 0; i < size - 1; ++i) { bool ordered = true; for (size_t j = 0; j < size - 1 - i; ++j) if (data[j+1] <

冒泡 选择 插入 希尔 堆 归并 快速 排序算法

排序相关概念 排序:对一序列对象根据某个关键字进行排序: 稳定:如果a原本在b前面,而a=b,排序之后a仍然在b的前面: 不稳定:如果a原本在b的前面,而a=b,排序之后a可能会出现在b的后面: 内排序:所有排序操作都在内存中完成: 外排序:由于数据太大,因此把数据放在磁盘中,而排序通过磁盘和内存的数据传输才能进行: 排序耗时的操作:比较.移动: 排序分类: (1)交换类:冒泡排序.快速排序:此类的特点是通过不断的比较和交换进行排序: (2)插入类:简单插入排序.希尔排序:此类的特点是通过插入的

常见排序算法详解(冒泡、选择、插入、快速、希尔、归并)

一.排序算法 1.冒泡排序(Bubble Sort) 定义:是一种简单的排序算法.它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成.这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端. 原理: 比较相邻的元素.如果第一个比第二个大(升序),就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.这步做完后,最后的元素会是最大的数. 针对所有的元素重复以上的

python排序算法实现(冒泡、选择、插入)

python排序算法实现(冒泡.选择.插入) python 从小到大排序 1.冒泡排序: O(n2) s=[3,4,2,5,1,9] #count = 0 for i in range(len(s)): for j in range((i+1),len(s)): s[i],s[j]=min(s[i],s[j]),max(s[i],s[j]) #print count print s 2.选择排序: O(n2) s=[3,4,2,5,1,9] #count = 0 for i in range(l

python 数据结构与算法之排序(冒泡,选择,插入)

目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 计算机重要的几门课: 1.数据结构和算法 2.网络 3.操作系统 4.计算组成原理 数据结构与算法: 算法: 衡量算法的标准: 时间复杂度:就是程序代码执行的大概次数 小结: 时间复杂度是用来估计算法运行时间的一个式子(单位) 一般来说,时间复杂度高的算法比复杂度低的算法慢 常见的

排序算法-(冒泡、选择、插入算法)

运行效率较慢的三大排序算法:冒泡.选择与插入算法,时间复杂度都为O(n2),运行效率较慢. #python 代码实现如下所示: li=[2,1,4,5,7,8,9,5,3]#冒泡算法(升序算法)O(n2)import randomfrom cal_time import *@cal_timedef bubble_sort(li): for i in range(len(li)-1): #第i趟 exchange=False for j in range(len(li)-i-1): #无序区域为n