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

哔哩哔哩数据结构讲解地址: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(array) / sizeof(int);
    cout<<"输入的原始序列:  ";
    for(int i=0; i<len; i++) // 输出原序列
        cout<<array[i]<<",";
    cout<<endl<<endl;
    cout<<"  ----冒泡排序过程如下---- " << endl;
    bubble(array,len); // 调用排序函数
    return 0;
}

void bubble(int a[], int size)
{
    // 冒泡排序具体的过程
    // 两个for循环,一个控制轮数,一个控制每轮比较的次数
    for(int pass=1; pass<size; pass++) // 比较的 size - 1 轮
    {
        for(int i=0; i<size-pass; i++) // 每轮比较的次数: size-pass
            if(a[i+1]<a[i])
            {
                swap(a[i],a[i+1]);
            }

        for(int j=0; j<size; j++)
            cout<<a[j]<<",";
        cout<<endl;
    }
}

选择排序

//
// Created by Tusdao_xxw on 2019/11/10.
//
# include<iostream>
using namespace std;
void selectSort(int[], int);

int main()
{
    int array [] = {55,2,6,4,32,12,9,73,26,37};

    int len = sizeof(array) / sizeof(int);

    cout<<"输入的原始序列:  ";
    for(int i=0; i<len; i++) // 输出原序列
        cout<<array[i]<<",";
    cout<<endl<<endl;

    cout<<"  ----选择排序开始---- " << endl;
    selectSort(array,len); // 调用排序函数
    return 0;
}
void selectSort(int a[], int size)
{
    int minIndex, temp;
    for(int i=0; i<size; i++)
    {
        minIndex=i;
        for(int j=i;j<size; j++)
        {
            if(a[minIndex]>a[j])
            {
                minIndex = j;
            }
        }
        temp = a[i];
        a[i] = a[minIndex];
        a[minIndex] = temp;
        for(int j=0; j<size; j++)
            cout<<a[j]<<",";
        cout<<endl;
    }
}

插入排序

#include<iostream>
using namespace std;
void isort(int[], int);

int main()
{
    int array [] = {55,2,6,4,32,12,9,73,26,37};

    int len = sizeof(array) / sizeof(int);

    cout<<"输入的原始序列:  ";
    for(int i=0; i<len; i++) // 输出原序列
        cout<<array[i]<<",";
    cout<<endl<<endl;

    cout<<"  ----插入排序开始---- " << endl;
    isort(array,len); // 调用排序函数
    return 0;
}

void isort(int a[], int size)
{
    int inserter, index;
    // 插入排序过程
    for(int i=1; i<size; i++)
    {
        inserter = a[i]; // 待插入的元素
        index = i - 1;  // 与待插入元素比较的元素下标,前i个元素已经排好顺序
        while(index>=0 && inserter < a[index]) // 找位置关键操作
        {
            a[index + 1] = a[index];
            index--;
        }
        a[index + 1] = inserter;
        for(int j=0; j<size; j++)
            cout<<a[j]<<",";
        cout<<endl;
    }
}

原文地址:https://www.cnblogs.com/xwxz/p/11867795.html

时间: 2024-07-29 02:51:59

第六章 排序-冒泡,选择,插入排序的相关文章

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

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

《数据结构与算法分析:C语言描述》复习——第六章“排序”——冒泡排序

2014.06.17 01:04 简介: 冒泡排序是O(n^2)级别的交换排序算法,原理简单,属于必知必会的基础算法之一. 思路: 排序要进行N轮,每一轮从尾部逐个向前扫描,遇到逆序对就进行交换.确保每一轮把最小的元素交换到前面去.这个过程好比水中的气泡向上飘,所以叫冒泡排序.代码非常简单,所以语言描述反而显得麻烦了. 实现: 1 // My implementation for bubble sort. 2 #include <iostream> 3 #include <vector&

基本排序-冒泡/选择/插入(python)

# -*- coding: utf-8 -*- import random def bubble_sort(seq): n = len(seq) for i in range(n-1): print(seq) for j in range(n-1-i): if seq[j] > seq[j+1]: seq[j], seq[j+1] = seq[j+1], seq[j] print(seq) def test_bubble_sort(): seq = list(range(10)) random.

第六章 常见排序算法

上章回顾 二叉树的定义 树深度的定义 什么样的二叉树是满二叉树 中序遍历的规则 [email protected]:Kevin-Dfg/[email protected]:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git 第六章 第六章 常见排序算法 常见排序算法 [email protected]:Kevin-Dfg/[email protected]:Kevin-Dfg/Data-Structures-and-Algorith

常见排序集合(冒泡排序,选择排序,直接插入排序,二分插入排序,快速排序,希尔排序,归并排序)

一下是一些常见的排序算法: 交换元素(后面算法都有用到): // 交换元素 private static void swap(int[] a, int i, int j) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } 冒泡排序(有优化): // 冒泡排序(优化①,②,③,④) private static void bubbleSort(int[] a) { boolean flag = false;// ①表示整个序列是无序的 for

排序算法学习之简单排序(冒泡排序,简单选择排序,直接插入排序)

一.冒泡排序 冒泡排序算是最基础的一种算法了,复杂度为O(N^2),其基本思想是:从最低端数据开始,两两相邻比较,如果反序则交换.代码如下: /*最基本的冒泡排序*/ void BubbleSort1 (int n, int *array) /*little > big*/ { int i, j; for (i=0; i<n-1; i++) { for (j=n-1; j>i; j--) { if (array[j] < array[j-1]) { int temp = array

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]以是最大值)

简单选择排序和直接插入排序

简单选择排序 简单选择排序就是通过关键字之间的比较,在记录里面找到最小(或者最大)的数字,并同当前位置交换之. 贴个代码: void SelectSort(SqList *L) { int i, j, min; for (i = 0; i < L->length - 1; i++) { min = i; for (j = i + 1; j < L->length; j++) { if (L->r[min] > L->r[j]) { min = j; } } if

php实现的冒泡,插入排序,希尔排序,归并排序

<?php/***选择排序也就是冒泡排序,就是基于各个数据之间的对比来排序**/$arr = array(2,1,7,5,8,9,3,4,10,30,28,24);function bubbleSort($arr) {    $len = count($arr);    $k = 0;    for($i=0;$i<$len;$i++){    $k++;        for($j=$i+1;$j<$len;$j++) {            if($arr[$i] > $ar