Java Foundation------Select sort

Source: Internet
Author: User

The idea of sorting is to compare the current data with all the data behind it, choose the smallest one, and put it in the current position.

Import Java.util.Arrays;
Import Java.util.Random;


Select sort
public class Selectsort {
public static void Main (string[] args) {
//1. Generates an array of random numbers between 1-100, with an array length of ten
Int[] Arr=new int[10];
Random ran=new random ();
for (int i=0;i<10;i++) {
Arr[i]=ran.nextint (100) +1;
System.out.print (arr[i]+ ",");
}
System.out.println ();


//2. Call Selection Sorting algorithm
Selectsort (arr);

  3. Output sorted Array
System.out.print (arrays.tostring (arr));
}

  

//Select sorting algorithm
public static void Selectsort (int[] arr) {

  The first number in the array is compared to the second-to-last number, and the outer-for-loop can be swapped from the smallest number in the data after Arr[i], and Arr[i].
for (int i=0;i<arr.length-1;i++) {

    Each data that is taken out of the external for Loop Arr[i] is compared to all the data behind it, and the smallest data arr[i] is swapped out from it.

    The scope of the inner for loop is the data from the outer for loop corresponding to the data Arr[i] begins until the last element of the array.
for (int j=i+1;j<arr.length;j++) {
if (Arr[j]<arr[i]) {
int temp=arr[i];
ARR[I]=ARR[J];
Arr[j]=temp;
}
}
}
}
}

Java Foundation------Select sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.