《算法导论》练习题--插入排序
来源:互联网 发布:mysql时间转换成毫秒值 编辑:程序博客网 时间:2024/06/05 05:56
习题:
2.1-1 以图2.2为模型,说明INSERTION-SORT在数组A=<31,41,59,26,41,58>上的执行过程
using System;using System.Collections.Generic;namespace InsertSort{ class Program { static void Main(string[] args) { Sort(); } private static void Sort() { Console.WriteLine("插入排序"); int[] arr = {31,41,59,26,41,58 }; Console.Write("排序前数组:"); foreach (int i in arr) { Console.Write(i+" "); } Console.WriteLine(); var length = arr.Length; for (int i = 1; i < length; i++) { for (int j = i; j > 0; j--) { if (arr[j-1]> arr[j]) { arr[j - 1] = arr[j] ^ arr[j - 1]; arr[j] = arr[j] ^ arr[j - 1]; arr[j - 1] = arr[j] ^ arr[j - 1]; } } PrintResult(arr); } } private static void PrintResult(IEnumerable<int> arr) { foreach (int i in arr) { Console.Write(i+" "); } Console.WriteLine(); } }}
运行结果:
2.1-4 考虑把两个n位二进制整数加起来的问题,这两个整数分别存储在两个n元数组A和B中。这两个整数的和应按二进制形式存储在一个(n+1)元数组C中。请给出该问题的形式化描述,并写出伪代码。
using System;using System.Collections.Generic;namespace Binary{ class Program { const int NUM = 8; static void Main(string[] args) { BinaryAdd(); } private static void BinaryAdd() { int[] a = new int[NUM] { 1, 1, 0, 1, 0, 1, 0, 1 };//213 int[] b = new int[NUM] { 1, 0, 0, 1, 1, 1, 0, 1 };//157 int[] c = new int[NUM + 1]; int flag = 0;//进位标识 for (int i = NUM; i > 0; --i) { c[i] = a[i - 1] + b[i - 1] + flag; if (c[i] > 1)//1或2 { c[i] = c[i] % 2; flag = 1; } else { flag = 0; } } c[0] = flag; foreach (int i in c) { Console.Write(i); } } }}
运行结果:
0 0
- 《算法导论》练习题--插入排序
- 算法导论:插入排序
- 插入排序【算法导论】
- 算法导论-----插入排序
- 算法导论-插入排序
- 《算法导论》 插入排序
- 【算法导论】插入排序
- 算法导论--插入排序
- 【算法导论】插入排序
- 《算法导论》插入排序
- 算法导论-插入排序算法
- 算法导论读书笔记(插入排序)
- [算法导论]2.1 插入排序
- 算法导论2.1插入排序
- 【算法导论】 插入排序实现
- 算法导论_插入排序
- 【算法导论】插入排序法
- 算法导论 2.1 插入排序
- UVA - 10249 The Grand Dinner
- 考虑十进制数
- 轻松自在,Word全角半角来回转
- 关于 Eclipse 创建 Dynamic Web Project (动态的 WEB 项目) 为什么会出现没有web.xml配置文件的问题
- poj-2823
- 《算法导论》练习题--插入排序
- Toxophily(纯粹的物理公式 + 一元二次方程)
- 设计模式中类的关系
- UVA - 107 The Cat in the Hat
- js中通过构造函数创建对象
- Nginx(二)通过SVN获得Nginx源码
- POJ 2253 Frogger
- IO流缓冲区对象的使用和理解。
- 数据库命令大全