Home > Java > javaTutorial > body text

java data structure sorting algorithm (4) selection sort

零下一度
Release: 2017-05-31 09:44:05
Original
1515 people have browsed it

This article mainly introduces the relevant information of Java data structure and algorithm selection sorting. This article explains it through the code. It is very good and has reference value. Friends who need it can refer to it

Each pass selects the smallest (or largest) element from the data elements to be sorted, and places it at the end of the sorted sequence until all the data elements to be sorted are arranged.

Code

public class ChoseSort { 
  //constructor without parameters 
  public ChoseSort(){}; 
  
  //constructor with parameters 
  public int[] ChoseSort(int[] intArr){ 
   for(int i=0;i<intArr.length-1;i++){ 
    int lowIndex = i; 
    
    for(int j=i+1;j<intArr.length;j++){ 
     if(intArr[j]<intArr[lowIndex]){ 
      lowIndex = j; 
     } 
    } 
    
    //将当前第一个元素与它后面序列中的最小的一个 元素交换,也就是将最小的元素放在最前端 
    int temp = intArr[i];    
    intArr[i] = intArr[lowIndex]; 
    intArr[lowIndex] = temp; 
   } 
   
   return intArr; 
  } 
  
  public static void main(String[] args) { 
   ChoseSort choseSort = new ChoseSort(); 
   int[] intArr = {5,6,9,2,4,3,8}; 
   int[] intArrAfterSort = choseSort.ChoseSort(intArr); 
   for(int arrItem:intArrAfterSort){ 
    System.out.print(arrItem+" "); 
   } 
  } 
 }
Copy after login

[Related recommendations]

##1.

Java data structure sorting algorithm (1) Tree selection sort

2.

java data structure sorting algorithm (2) merge sort

3.

java data structure sorting algorithm (3) simple selection sort

4.

Detailed tutorial on selection sorting (Selection Sort_java) in Java

The above is the detailed content of java data structure sorting algorithm (4) selection sort. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template