In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "JavaScript, Python, Java implementation selection sorting code how to write", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "JavaScript, Python, Java to achieve the selection of sorting code how to write it!"
Selective sorting (Selection sort) is a simple and intuitive sorting algorithm. The basic idea is: first find the smallest (or maximum) element in the unsorted sequence, and then store it at the beginning of the sequence; then, continue to find the smallest (or maximum) element from the remaining unsorted elements, and then put it at the end of the sorted sequence.
Algorithm step
First, find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence.
Continue to look for the smallest (largest) element from the remaining unsorted elements, and then put it at the end of the sorted sequence.
Repeat the second step until all the elements are sorted.
Implementation of dynamic diagram demonstration code
JavaScript code implementation
Example
Function selectionSort (arr) {var len = arr.length; var minIndex, temp; for (var I = 0; I for (var j = I + 1; j if (arr [j] return arr;})
Python code implementation
Example
Def selectionSort (arr): for i in range (len (arr)-1): # Index of the minimum number minIndex = i for j in range (I + 1, len (arr)): if arr [j] # I is not the minimum number, exchange I with the minimum number if I! = minIndex: arr [I], arr [minIndex] = arr [minIndex], arr [I] return arr
Go code implementation
Example
Func selectionSort (arr [] int) [] int {length: = len (arr) for I: = 0; i for j: = I + 1; j if arr [min] > arr [j] {min = j}} arr [I], arr [min] = arr [min], arr [I]} return arr}
Java code implementation
Example
Public class SelectionSort implements IArraySort {@ Override public int [] sort (int [] sourceArray) throws Exception {int [] arr = Arrays.copyOf (sourceArray, sourceArray.length); / / one round of comparison for (int I = 0; I for (int j = I + 1; j if (arr [j] if (I! = min) {int tmp = arr [I]; arr [I] = arr [min]) Arr [min] = tmp;}} return arr;}}
PHP code implementation
Example
Function selectionSort ($arr) {$len = count ($arr); for ($I = 0; $I $len-1; $iTunes +) {$minIndex = $I; for ($j = $I + 1; $j $len; $jacks +) {if ($arr [$j] $arr [$minIndex]) {$minIndex = $j;} $temp = $arr [$I]; $arr [$I] = $arr [$minIndex]; $arr [$minIndex] = $temp } return $arr;}
C language
Example
Void swap (int * a for int * b) / / two variables {int temp = * a; * a = * b; * b = temp;} void selection_sort (int arr [], int len) {int iJournal j; for (I = 0; I for (j = I + 1; j if (arr [j])
C++
Example
Template / / integers or floating point numbers can be used. To use an object (class), you must set the operator function void selection_sort (std::vector& arr) {for (int I = 0; I for (int j = I + 1; j if (arr [j])) greater than (>).
C#
Example
Static void selection_sort (T [] arr) where T: System.IComparable {/ / integer or floating point number can use int I, j, min, len = arr.Length; T temp; for (I = 0; I for (j = I + 1; j if (arr [min] .Compareto (arr [j]) > 0) min = j; temp = arr [min] Arr [min] = arr [I]; arr [I] = temp;}}
Swift
Example
Import Foundation/// Select sort /-Parameter list: array func selectionSort (_ list: inout [Int])-> Void {for j in 0..for I in j..if list [minIndex] > list [I] {minIndex = I}} list.swapAt (j, minIndex)}} so far I believe that you have a deeper understanding of how to write the code for JavaScript, Python, and Java to implement selective sorting, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.