Java Data structure (sort article)

Source: Internet
Author: User

Bubble Sort : It is after n-1 the order of the sub-ordering, the first sub-order from the 1th number to the number of n-i, if the number of I is greater than the next number (then ascending, small, descending) the exchange of two number. Big bubbles on the top, small bubbles in the next.


Select sort : Each trip selects the smallest (or largest) element from the data element to be sorted, placing the order at the end of the ordered sequence until all the data elements to be sorted are finished. Select Sort is an unstable sort method.
Insert Sort : There is an ordered sequence of data that requires a number to be inserted into the sorted data sequence, but this data sequence is still ordered after insertion, and a new sorting method is used-the insertion sort method, The basic operation of inserting a sort is to insert a data into the ordered data that is already sorted, so as to get a new sequential data with a number plus one, the algorithm is suitable for the ordering of a small amount of data. The time complexity is O (n^2). is a stable sorting method. The insertion algorithm divides the array to be sorted into two parts: the first part includes all the elements of the array, except for the last one, and the second part contains only this element. After the first part is sorted. Then insert this last element into the position that is already in the first part of the order.


Package com.google.datastruct;

public class Sortarray {
Array
Private long[] arr;
Effective length
private int elems;
Default constructor method
Public Sortarray () {
arr = new LONG[50];
}
Public Sortarray (int max) {
arr = new Long[max];
}
Inserting data
public void Insert (Long value) {
Arr[elems] = value;
elems++;
}
Show data
public void display () {
for (int i = 0; i < Elems; i++) {
System.out.print (Arr[i] + "");
}
System.out.println ();
}
Bubble sort
public void Bubsort () {
Long temp = 0L;
for (int i = 0; i < elems-1; i++) {
for (int j = 0; J < elems-i-1; j + +) {
if (Arr[j] > arr[j + 1]) {
temp = Arr[j];
ARR[J] = arr[j + 1];
Arr[j + 1] = temp;
}
}
}
}
Select sort
public void Selectsort () {
for (int i = 0; i < elems-1; i++) {
Long temp = 0L;
int min = i;
for (int j = i + 1; j < Elems; J + +) {
if (Arr[j] < arr[min]) {
min = j;
}
}
temp = Arr[i];
Arr[i] = Arr[min];
Arr[min] = temp;
}
}
Insert Sort
public void Insertsort () {
Long select = 0L;
for (int i = 1; i < Elems; i++) {
select = Arr[i];
int j = 0;
for (j = i; j > 0 && arr[j-1] >= Select; j--) {
ARR[J] = arr[j-1];
}
ARR[J] = select;
}
}

}

Test

public class Testdemo {
public static void Main (string[] args) {
Sortarray sort = new Sortarray ();
Sort.insert (355);
Sort.insert (63);
Sort.insert (66);
Sort.insert (85);
Sort.insert (494);
Sort.insert (124);
Sort.display ();

Sort.bubsort (); Bubble sort
Sort.selectsort (); Select sort
Sort.insertsort (); Insert Sort
Sort.display ();
}
}

Java Data structure (sort article)

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.