标签:pre author http name 实现 vat 技术分享 ring version
一、C 程序实现
/******************************************************* * Description: 直接选择排序 * Author: shujuxiong * Version: 1.0 * Time: 2018-06-24 *******************************************************/ #include <stdio.h> //函数:打印数组 void PrintDataArray(int a[], int n) { for(int i=0; i<n; i++) printf("%d ",a[i]); printf("\n"); } void SelectSort(int a[], int n) { for(int i = 0; i < n; i++) { int min = i; for(int j = i+1; j < n; j++) { if(a[j] < a[min]) min = j; } int tmp = a[i]; a[i] = a[min]; a[min] = tmp; } PrintDataArray(a, n); } //测试用例 int main() { int a[] = {3,1,7,5,2,4,9,6}; int len = sizeof(a)/sizeof(a[0]); SelectSort(a, len); return 0; }
运行结果:
二、Java 程序实现
/** * @description: 直接选择排序算法 * @author: shujuxiong * @version: 1.0 * @date: 2018-06-24 */ public class SelectSort { public static void sort(int[] a) { int N = a.length; for(int i = 0; i < N; i++) { int min = i; for(int j = i+1; j < N; j++) { if(a[j] < a[min]) min = j; } int tmp = a[i]; a[i] = a[min]; a[min] = tmp; } } //方法:打印数组 private static void printDataArray(int[] a) { int N = a.length; for(int i = 0; i < N; i++) System.out.printf("%d ",a[i]); System.out.printf("\n"); } //测试用例 public static void main(String[] args) { int[] arr = {3,1,7,5,2,4,9,6}; sort(arr); printDataArray(arr); } }
运行结果:
三、Python 程序实现
# -*- coding: utf-8 -*- """ Description: 直接选择排序算法 Author: shujuxiong Version: 1.0 Date: 2018-06-24 """ import copy ##选择排序 def selectSort(relist): N = len(relist) for i in range(N): min = i; for j in range(i+1,N): if relist[j] < relist[min]: min = j tmp = relist[i]; relist[i] = relist[min] relist[min] = tmp return relist ##测序用例 def main(): mylist = [3,1,7,5,2,4,9,6] print(selectSort(copy.copy(mylist))) if __name__==‘__main__‘: main()
运行结果:
标签:pre author http name 实现 vat 技术分享 ring version
原文地址:https://www.cnblogs.com/shujuxiong/p/9221242.html