C# 字符串,数字的排序重载
来源:互联网 发布:js文件怎么运行网页 编辑:程序博客网 时间:2024/06/06 00:56
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp4{ class Program { static void Main(string[] args) { int[] a = { 2, 3, 5, 8, 9, 1, 0 }; double[] b = { 1.2, 3.4, 5.6, 7.8, 9, 1 }; string[] s = {"China","Japan","Viet","Abc"}; string[] s2 = { "student", "teacher", "worker", "clerk" }; Sort sort = new Sort(); int []aSort = sort.BubbleSort(a); Console.Write("a数组的排序结果为:\n"); for (int i = 0;i<aSort.Length ;i++) { Console.Write(aSort[i]); Console.Write(" "); } Console.Write("\nb数组的排序结果为:\n"); double[] bSort = sort.BubbleSort(b); for (int i = 0; i < bSort.Length; i++) { Console.Write(bSort[i]); Console.Write(" "); } Console.Write("\ns数组的排序结果为:\n"); string[] sSort = sort.BubbleSort(s); for (int i = 0; i < sSort.Length; i++) { Console.Write(sSort[i]); Console.Write(" "); } Console.Write("\n数据列表数组的排序结果为:\n"); string[] s2Sort = sort.BubbleSort(s2); for (int i = 0; i < s2Sort.Length; i++) { Console.Write(s2Sort[i]); Console.Write(" "); } Console.ReadLine(); } } class Sort { public int[] BubbleSort(int[] a) { int[] b = new int[a.Length]; for(int k=0;k<a.Length ;k++) { b[k] = a[k]; } for(int i =0; i<b.Length;i++) { for(int j = 0;j<=b.Length-2-i ;j++) { if(b[j]<b[j+1]) { int temp = b[j]; b[j] = b[j + 1]; b[j + 1] = temp; } } } return b; } public double[] BubbleSort(double[] a) { double[] b = new double[a.Length]; for (int k = 0; k < a.Length; k++) { b[k] = a[k]; } for (int i = 0; i < b.Length; i++) { for (int j = 0; j <= b.Length - 2 - i; j++) { if (b[j] < b[j + 1]) { double temp = b[j]; b[j] = b[j + 1]; b[j + 1] = temp; } } } return b; } public string[] BubbleSort(string[] a) { string [] b = new string[a.Length]; for (int k = 0; k < a.Length; k++) { b[k] = a[k]; } for (int i = 0; i < b.Length; i++) { for (int j = 0; j <= b.Length - 2 - i; j++) { char m = char.Parse(b[j].Substring(0, 1)); char n = char.Parse(b[j + 1].Substring(0, 1)); if ((int)m>(int)n) { string temp = b[j]; b[j] = b[j + 1]; b[j + 1] = temp; } } } return b; } }}
构造一个类,可以分别对任意多个整数、小数或字符串进行排序。
(1)创建C#控制台应用程序L3_4
(2)在main()方法中,有int[] a={2,3,5,8,9,1,0},double[] b={1.2,3.4,5.6,7.8,9,1},string[]s={“China”,”Japan”,”Viet”,”Abc”}三个数组。
(3)定义一个类Sort,利用BubbleSort方法实现三种类型数据的排序。
(4)利用数据列表(23.34.66.45.11.6),对排序功能来进行测试。
(5)利用数据列表(“student”,”teacher”,”worker”,”clerk”),对排序功能进行测试。
阅读全文
0 0
- C# 字符串,数字的排序重载
- C#字符串数组中含数字时的排序
- MySQL字符串中数字排序的问题
- 字符串类型的数字从小到大排序方法
- 如何排序含有数字的字符串
- MySQL字符串中数字排序的问题
- MySQL字符串中数字排序的问题
- MySQL字符串中数字排序的问题
- C#巧截字符串的数字
- C#生成唯一的字符串或者数字
- C# 字符串和数字转换的函数
- .SQL 字符串+数字的字段按数字排序
- DataTable字符串类型的数字,按照数字类型排序
- SQL 数字字符串 排序
- 字符串内数字排序
- 字符串按数字排序
- c# 判断字符串数字
- c# 字符串转换数字
- 两种无参考空间域图像噪点检测方法
- [4]380. Insert Delete GetRandom O(1)(Java)
- getDrawable被划
- MFC程序中启动其他程序,并带参数
- python中ASCII码字符与int之间的转换
- C# 字符串,数字的排序重载
- flume的source, channel, sink 列表
- java中Map、JSONArray、JSONObject的关系及转换以及json字符串与json对象的转化
- AJAX 跨域请求
- 从0开始,开发一款属于自己的Android Studio插件
- JavaScript 内存泄漏教程
- Tomcat和http协议简介
- 面向对象 __unset 魔术方法
- python获取服务器响应cookie