Lapping data structures and algorithms-02 various sorting algorithms

Source: Internet
Author: User

One, bubble sort method


public class Bubblesort {

public static long[] Sort (long[] arr) {

Long temp;

for (int i=0;i<arr.length-1;i++) {

for (int j=arr.length-1;j>i;j--) {

if (Arr[j]<arr[j-1]) {

TEMP=ARR[J];

ARR[J] = arr[j-1];

Arr[j-1]=temp;

}

}

}

return arr;

}

}

Test:

public class Testsort {

public static void Main (string[] args) {

long[] arr = new LONG[5];

arr[0]=34;

arr[1]=23;

arr[2]=2;

Arr[3]=1;

System.out.println ("[");

for (long Num:arr) {

System.out.println (num+ "");

}

System.out.println ("]");

System.out.println ();

arr = Bubblesort.sort (arr);

System.out.println ("[");

for (long Num:arr) {

System.out.println (num+ "");

}

System.out.println ("]");

System.out.println ();

}

}

Two: Select sort

public class Selectionsort {


public static long[] Sort (long[] arr) {

int k=0;

Long temp;

for (int i=0;i<arr.length-1;i++) {

K=i;

for (int j=i;j<arr.length;j++) {

if (Arr[j]<arr[k]) {

K=j;

}

}

Temp=arr[i];

ARR[I]=ARR[K];

Arr[k]=temp;

}

return arr;

}

}

Test:

public class Testsort {


public static void Main (string[] args) {

long[] arr = new LONG[5];

arr[0]=34;

arr[1]=23;

arr[2]=2;

Arr[3]=1;

Arr[4]=-1;

System.out.println ("[");

for (long Num:arr) {

System.out.println (num+ "");

}

System.out.println ("]");

System.out.println ();

arr = Selectionsort.sort (arr);

System.out.println ("[");

for (long Num:arr) {

System.out.println (num+ "");

}

System.out.println ("]");

System.out.println ();

}

}

Three: Insert sort


public class Insertsort {


public static long[] Sort (long[] arr) {

Long temp=0;

for (int i=1;i<arr.length-1;i++) {

Temp=arr[i];

int j=1;

while (j<0&&arr[j]>=temp) {

ARR[J]=ARR[J-1];

j--;

}

Arr[j]=temp;

}

return arr;

}

}

Test:

public class Testsort {


public static void Main (string[] args) {

long[] arr = new LONG[5];

arr[0]=34;

arr[1]=23;

arr[2]=2;

Arr[3]=1;

Arr[4]=-1;

System.out.println ("[");

for (long Num:arr) {

System.out.println (num+ "");

}

System.out.println ("]");

System.out.println ();

arr = Insertsort.sort (arr);

System.out.println ("[");

for (long Num:arr) {

System.out.println (num+ "");

}

System.out.println ("]");

System.out.println ();

}

}


Lapping data structures and algorithms-02 various sorting algorithms

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.