C++、C#、java算法学习日记02----选择排序(SelectSort)
来源:互联网 发布:抚仙湖水下古城 知乎 编辑:程序博客网 时间:2024/05/19 05:04
直接选择排序属于排序算法的一种,他的排序速度要比冒泡排序快一些,算是对冒泡排序的一种改进。
基本思想:
直接排序的思想类似于我们实际生活中的排序行为,比如:对一串数字 63,4,24,1,3,15从小到大排序,我们会首先找到最大的值与最后一位交换位置,然后再从余下的数中找到最大的值与倒数第二位交换位置,这样每次都从余下的数中找到最大的放到末尾,当余下一个数时排序完成
C++实例:
#include<iostream>using namespace std;void ShowArray(int *array,int Length);void SelectSort(int *array,int Length){ for(int i=1;i<Length;i++) //控制循环趟数 { int index=0; //循环找到最大值 for(int j=1;j<=Length-i;j++) { if(array[j]>array[index]) { index=j; } } //将最大值放到末尾 int temp; temp=array[index]; array[index]=array[Length-i]; array[Length-i]=temp; } ShowArray(array,Length);}//输出函数void ShowArray(int *array,int Length){for(int i=0;i<Length;i++){ cout<<array[i] <<" ";}cout<<endl;}void main(){int array[]={63,4,24,1,3,15};int Length=sizeof(array)/sizeof(array[0]); //获取数组长度SelectSort(array,Length);}
C#实例:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace SelectSort{ class Sort { public void SelectSort(int[] array) { for (int i = 1; i < array.Length; i++) { int index = 0; for (int j = 1; j <= array.Length - i; j++) { if (array[j] > array[index]) { index = j; } } int temp; temp = array[index]; array[index] = array[array.Length - i]; array[array.Length - i] = temp; } ShowArray(array); } public void ShowArray(int[] array) { foreach (int i in array) { Console.Write(i + " "); } Console.WriteLine(); } static void Main(string[] args) { Sort sorter = new Sort(); int[] array = new int[] {63,4,24,1,3,15}; sorter.SelectSort(array); } }}
以上结果都样:
java实例:
package Sort;public class SelectSort {public static void main(String[] args) {int[] array ={63,4,24,1,3,15};SelectSort sorter = new SelectSort();sorter.sort(array);}public void sort(int[] array){int index;for(int i=1;i<array.length;i++){index=0;for(int j=1;j<=array.length-i;j++){if(array[j]>array[index]){index=j;}}int temp = array[array.length-i];array[array.length-i]=array[index];array[index]=temp;}showArray(array);}public void showArray(int[] array){for(int i:array){System.out.print(" "+i);}System.out.println();}}
结果:
0 0
- C++、C#、java算法学习日记02----选择排序(SelectSort)
- 选择排序及C++/java代码实现(SelectSort)
- [算法 java] 选择排序selectSort
- Java--选择排序(SelectSort)
- 排序算法-SelectSort-选择排序
- 选择排序(SelectSort)
- C++、C#、java算法学习日记03----插入排序
- C++、C#、java算法学习日记04----二分插入排序
- 数据算法之选择排序(selectSort)的Java实现
- 选择排序(selectSort.cpp)
- C++、C#、java算法学习日记01---冒泡排序(BubbleSort)
- C++、C#、java算法学习日记05----希尔排序(ShellSort)
- C++、C#、java算法学习日记06----堆排序思想(HeapSort)
- C++、C#、java算法学习日记07----堆排序(HeapSort)
- 选择排序(Selectsort)之Java实现
- 选择排序(Selectsort)之Java实现
- 排序——选择排序(Selectsort)
- 排序(二)之选择排序SelectSort
- iOS多线程编程
- 理解sychronized针对的是对象而非语句或者方法
- 单例设计模式详解
- iOS和Android的app界面设计规范
- python+flask+SAE 微信公共平台开发的小小的demo
- C++、C#、java算法学习日记02----选择排序(SelectSort)
- thinkphp 数据库字段自增自减
- iOS自定义导航控制器侧滑返回的多种实现方法
- android五种布局模式
- 【Objective-C Runtime】之理解
- CAS单点登录(SSO)完整教程
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片的优缺点比较
- textView输入字数统计
- android 仿蘑菇街喜欢