java select sort insertion sort (and the use of arrays)

2. Selection Sort

Selection-sort is a simple and intuitive sorting algorithm. How it works: First find the smallest (large) element in the unsorted sequence, store it at the beginning of the sorted sequence, and then continue to find the smallest (large) element from the remaining unsorted elements, and then put it in the sorted sequence At the end. And so on, until all elements are sorted.

2.1 Algorithm description

Direct selection and sorting of n records can be ordered and sorted through n-1 trips to obtain ordered results. The specific algorithm is described as follows:
• Initial state: the unordered area is R [1… n], and the ordered area is empty;
• At the beginning of the i-th order (i = 1,2,3 ... n-1), the current order is The area and disordered area are R [1 ... i-1] and R (i ... n), respectively. This sort of pass selects the record R [k] with the smallest key from the current unordered area, and exchanges it with the first record R in the unordered area, so that R [1 ... i] and R [i + 1 ... n) Become a new ordered area where the number of records increases by one and a new unordered area where the number of records decreases by one;
• The n-1 pass ends and the array is ordered.
Insert picture description here
Class practice
Question: Sort and
analyze the results of 8 classmates : divide the entire sequence into an ordered area and an unordered area, select the smallest amount to be in front, and then compare the two numbers, the smaller number continues to In the post ratio, the penultimate smallest amount is selected after one round and placed in the second position
so that the ordered area gradually increases.

int []a= {80,60,90,45,100,88,59,98};
	Sortl(a);
	}
	static void select Sort(int[]a) {		
for (int i=0;i<a.length-1;i++) {int min=1;
for (int j=i+1;j<a.length;j++) {
	if (a[min])>a[j] {min=j;}}
if(min!=0) {int temp;
          temp=a[i];
          a[i]=a[min];
          a[min]=temp;
}}

Insert sorting
Insert (Sertion-Sort) algorithm description is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence. For unsorted data, it scans back through the sorted sequence, finds the corresponding position and inserts it.

Algorithm Description

In general, insertion sorting is implemented on arrays using in-place. The specific algorithm is described as follows:
• Starting from the first element, the element can be considered to have been sorted;
• The next element is taken out and scanned from back to front in the sequence of already sorted elements;
• If the element (sorted) is greater than new Element, move the element to the next position;
• repeat step 3 until you find the position where the sorted element is less than or equal to the new element;
• after inserting the new element into this position;
• repeat steps 2 ~ 5.
Insert picture description here
Analysis: The first element has been sorted by default, starting from the next element and compared with the number that has been taken, after the greater than the new element, until the less than or equal to the element, the new element is inserted into the back, so as to loop the
code

int []a= {80,60,90,45,100,88,59,98};
	insertsort(a);
}
	static   void   insertsort(int[]a) {
		for (int i=0;i<a.length-1;i++) {int current=a[i+1];
		int j=i;
		while (j>0 && a[j]>current {a[j+1]=a[j];j--;}
		a[j+1]=current;
		}

Published 28 original articles · Like1 · Visits1706

Guess you like

Origin blog.csdn.net/qq_45870494/article/details/103338529