Select sort----Direct Select sort----Java implementation

Source: Internet
Author: User

Ideas:

First comparison: The program locates the record in the first position of the array, comparing the first data with each subsequent data,

Use a variable mix to record the index of data smaller than the first number, by comparing constantly updating the mix, and finally getting the index of the smallest element in the entire array, swapping the first number with the number of mix, the smallest digit is in the position of the array,

Second comparison: The program locates the record in the second position of the array, comparing the second data with each subsequent data,

Gets the smallest number in the array, starting with the second data, and swapping positions with the second number.

For a total of n-1 comparisons, each time a minimum number is selected in the remaining unordered array.

public void Selectsort (int[] array)
  {
	  int arraylength = array.length;
	 for (int i = 0; i < arrayLength-1 i++)
	{
		 //mix index value used to hold the smallest element in the array
		int mix = i;//select the current array element as the minimum, traverse to the last, find the smallest array element The index of the element, and the current position exchange for
		(int j = i+1 J < arraylength; J +)
		{
			if (ARRAY[J) < Array[mix])
			{
				mix = J;
 }///////
		Maximum swap
		if (mix!= i)//if mix is not equal to current I, let the value of the minimum and current position exchange
		{
			swap (array,mix,i);
		}
			
	}
  }
  private void Swap (int[] array,int mix,int i)
  {
	  int temp = Array[mix];
	  Array[mix] = Array[i];
	  Array[i] = temp;
  }


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.