【数据结构】C# 面向对象思想完成三种排序
来源:互联网 发布:sublimetext3 python 编辑:程序博客网 时间:2024/06/05 17:21
sort类 主要逻辑
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Selection{ public class sort { /** *selection sort 原理 * 默认升序排列 * 由循环找出最小值 * 与冒泡排序不同的是--》记录下最小值的下标 而不是每比较一次交换一次 * 然后把此次循环的最小值放到数组前面 */ public int[] selectionsort(int[] a, int n) { for (int j = 0; j < n - 1; j++) { int min_index = j;//循环令最小值的下标为j for (int i = j; i < n; i++) { if (a[i] < a[min_index]) { min_index = i;//记录下最小值的下标 } } int temp = a[j]; a[j] = a[min_index]; a[min_index] = temp; Display(a, n); } return a; } /** *冒泡排序 * 双重循环 比较 */ public int[] bubblesort(int[] a, int n) { for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (a[i] > a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } Display(a, n); } return a; } //插入排序 public int[] insertionsort(int[] a, int n) { for (int i = 1; i < n; i++) { int j = i - 1; // while 中条件的顺讯有先后 // 当j不满足就不进行a[j] 与 a[j + 1]的判断了 // 否则会越界.... while (j >= 0 && a[j] >= a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; j--; } Display(a, n); } return a; } public void inputdata(int[] a, int n) { for (int i = 0; i < n; i++) { Console.WriteLine("please input the data u want to input!"); a[i] = Int32.Parse(Console.ReadLine()); } } //打印 public void print(int[] a, int n) { Console.WriteLine(); for (int i = 0; i < n; i++) { Console.WriteLine("第" + (i+1) + "个数为:" + a[i]); } } //表现排序过程的打印 public void Display(int[] a, int n) { Console.WriteLine("\n排序过程 : "); for (int i = 0; i < n; i++) { Console.Write(a[i] + " "); } } //操作菜单 public void show() { Console.WriteLine("++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("1.selectionsort"); Console.WriteLine("2.bubblesort"); Console.WriteLine("3.insertionsort"); Console.WriteLine("++++++++++++++++++++++++++++++++++++++"); } } }
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Selection{ class Test { static void Main(string[] args) { Console.WriteLine("please input the array size"); int n = Int32.Parse(Console.ReadLine()); int[] a = new int[n]; //储存 int[] arr = new int[20]; int length = n; sort test = new sort(); test.inputdata(a, n); Array.Copy(a, 0, arr, 0, length); char ch; do { Console.Clear(); int key; test.show(); Console.WriteLine("请输入您要进行的操作"); key = Int32.Parse(Console.ReadLine()); switch (key) { case 1: a = test.selectionsort(a, n); break; case 2: a = test.bubblesort(a, n); break; case 3: a = test.insertionsort(a, n); break; default: break; } test.print(a, n); // Console.WriteLine("数组未排序前为:"); // test.print(arr, n); a = arr; Console.WriteLine("您想要继续吗?Y/y"); ch = Char.Parse(Console.ReadLine()); } while ('y' == ch || 'Y' == ch); } }}
阅读全文
0 0
- 【数据结构】C# 面向对象思想完成三种排序
- C#面向对象的思想
- C# 面向对象的思想
- 面向对象思想的三种通俗解释
- lua(三)面向对象思想
- java初学(三) 面向对象设计思想
- 跳出面向对象思想(三) 封装
- 二、java面向对象三大思想
- C#面向对象思想深刻剖析
- c#学习 9/22 面向对象思想
- 数据结构之循环队列(面向对象思想c++实现)
- C#实现面向对象多态性的三种方式
- java面向对象思想和三大特性
- (三)3面向对象思想和lseek
- Java面向对象思想及其三大特性
- 面向对象程序设计Java-三大特征和设计思想
- 【Java基础 三】---面向对象思想(高级)
- C#的类与结构,以及面向对象编程思想
- 两数组的交
- 海岛争霸
- 高德地图集成之基础定位
- CentOS安装Dubbo管理控制台
- InvalidDataAccessApiUsageException SpringMVC Spring Hibernate 整合
- 【数据结构】C# 面向对象思想完成三种排序
- java中路径的区分
- 网页qq客服链接
- 编译imu_3dm_gx4-master 节点编译错误
- redis集群java实践问题记录
- opencv学习笔记(3) 锐化图像 邻域操作 ROI
- 进程与线程杂记
- Java中常用的数组排序算法
- 网卡驱动之02驱动源码分析