插入排序2.0

好久没复习以前写的数据结构了,今天看了一段以前写的插入排序。

插入排序原理:通俗讲,和打扑克牌整理牌是一个道理,从一堆混乱牌中,一张一张拿,拿一张后与前面排好的牌一个个比较,插入合适的位置。

时间复杂度:O(n^2)

/*插入排序2.0
**此版本将创建数组函数删除,
**创建数组与插入排序编排到
**同一个函数里。
*/

#include "stdafx.h"
#include <stdio.h>

#define M 1000

void Insert_sorting(int a[],int n);//插入排序
void Out_put(int a[], int n);//输出数组

int main()
{
    int n;
    int a[M] = {0};

    printf_s("输入待排序个数\n");
    printf_s("n=");
    scanf_s("%d", &n);

    Insert_sorting(a,n);
    Out_put(a, n);

    return 0;
}

void Insert_sorting(int a[], int n)
{
    int i, j, wep;//wep is 中间变量

    printf_s("请输入第1个值=");
    scanf_s("%d", &a[0]);

    for (i = 1; i < n; i++)
    {
        printf_s("请输入第%d个值=", i+1);
        scanf_s("%d", &a[i]);

        for (j = i;; j--)
        {
            if (j == 0)
            {
                break;
            }

            if (a[j] < a[j - 1])
            {
                wep = a[j - 1];
                a[j - 1] = a[j];
                a[j] = wep;
            }
            else
            {
                break;
            }
        }
    }
}
void Out_put(int a[], int n)
{
    int i;

    for (i = 0; i < n; i++)
    {
        printf_s("%d   ", a[i]);
    }
}
时间: 2024-08-03 16:46:41

插入排序2.0的相关文章

&lt;每日一题&gt;题目27:插入排序(假)

''' 插入排序:假设元素左侧全部有序,找到自己的位置插入 ''' import random import cProfile def insert_sort(nums): for i in range(1,len(nums)): for j in range(i,0,-1): if nums[j-1] > nums[j]: nums[j-1],nums[j] = nums[j],nums[j-1] return nums nums = [random.randint(0, 10000) for

排 序 算 法

一.插入排序(Insertion Sort)1. 基本思想:  每次将一个待排序的数据元素,插入到前面已经排好序的数列中的适当位置,使数列依然有序:直到待排序数据元素全部插入完为止.2. 排序过程: [示例]:[初始关键字] [49] 38 65 97 76 13 27 49    J=2(38) [38 49] 65 97 76 13 27 49    J=3(65) [38 49 65] 97 76 13 27 49    J=4(97) [38 49 65 97] 76 13 27 49 

排序算法——C#语言版

插入排序:(直接插入排序&希尔排序) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sort { public class InsertSort { public static int k; /// <summary> /// 直接插入排序 /// </summary> /// <param name="a&quo

排序算法——C#语言

插入排序:(直接插入排序&希尔排序) using System; using System.Collections.Generic; using System.Linq; using System.Text; ? namespace Sort { public class InsertSort { public static int k; ? /// <summary> /// 直接插入排序 /// </summary> /// <param name="a

选择、冒泡、插入、直接排序算法

1 public class Demo { 2 3 public static void main(String[] args) { 4 int[] arr = new int[10]; 5 for (int i = 0; i < arr.length; i++) { 6 Random rd = new Random(); 7 arr[i] = rd.nextInt(100); 8 } 9 System.out.println("需排序数组-->"); 10 for (in

鬃嘴释怀说太多就成真不了。

子阻撞砖奏尊仔籽着 释怀说太多就成真不了. http://passport.baidu.com/?business&un=vip&un=%E5%A4%A9%E6%B0%B4%E4%B8%8A%E9%97%A8%E8%BF%99%E5%B0%8F%E5%A7%90#0 http://passport.baidu.com/?business&un=vip&un=%E7%99%BD%E9%93%B6%E4%B8%8A%E9%97%A8%E8%BF%99%E5%B0%8F%E5%A

澜星粘鼐贩逊耐盼系甭妊倏纪傲傲sdfghjk

http://passport.baidu.com/?business&un=R&un=%E5%A4%A7%E5%AE%81%E6%A1%91%E6%8B%BF%E9%80%9A%E5%B0%8F%E5%A7%90#0 http://passport.baidu.com/?business&un=R&un=%E4%B9%A1%E5%AE%81%E6%A1%91%E6%8B%BF%E9%80%9A%E5%B0%8F%E5%A7%90#0 http://passport.bai

在Ubuntu14.04上OpenStack Juno安装部署

在Ubuntu14.04上OpenStack Juno安装部署 0 安装方式 0.1 安装方式 安装方式 说明 目标 备注 单结点 一台服务器运行所有的nova-xxx组件,同时也驱动虚拟实例. 这种配置只为尝试Nova,或者为了开发目的进行安装.   1控制节点+N个计算节点 一个控制结点运行除nova-compute外的所有nova-services,然后其他compute结点运行nova-compute.所有的计算节点需要和控制节点进行镜像交互,网络交互,控制节点是整个架构的瓶颈. 这种配

2008 SCI 影响因子(Impact Factor)

Excel download 期刊名缩写 影响因子 ISSN号 CA-CANCER J CLIN 74.575 0007-9235 NEW ENGL J MED 50.017 0028-4793 ANNU REV IMMUNOL 41.059 0732-0582 NAT REV MOL CELL BIO 35.423 1471-0072 PHYSIOL REV 35.000 0031-9333 REV MOD PHYS 33.985 0034-6861 JAMA-J AM MED ASSOC 3