Summary of common sorting algorithms

Source: Internet
Author: User
Tags array length

Package com.company;
Import Java.util.Arrays;
Import Java.util.Scanner;
public class Arraysdemo {
public static void Main (string[] args) {

}
}
Class bubblesort{
/*
Bubble sort
Requirement: Given an array, order the two numbers adjacent to it, and output the maximum and minimum values.
Example: -1,54,36,89,0,45
First round: -1,54,36,89,0,45
-1,36,54,89,0,45
-1,36,54,89,0,45
-1,36,54,0,89,45
-1,36,54,0,45,89
Second round: -1,36,54,0,45,89
-1,36,54,0,45,89
-1,36,0,54,45,89
-1,36,0,45,54,89
Third round: -1,36,0,45,54,89
-1,0,36,45,54,89
-1,0,36,45,54,89

*/
public static void Main (string[] args) {
Defining an Integer array
Int[] arr = {0, 154, 9, 89,-1, 45};
PrintArray (arr);
Arrays.sort (arr);
PrintArray (arr);
Defines a variable that accepts the return value of a Getmax call
int max = Getmax (arr);
int min = getmin (arr);
System.out.println ("max=" + max); Souf shortcut keys
System.out.println ("min=" +min);

}
Print array
public static void PrintArray (int[] arr) {
for (int x=0;x<arr.length; x + +) {
if (x!=arr.length-1) {
System.out.print (arr[x]+ ",");
}else{
System.out.println (Arr[x]);
}
}
}
Gets the maximum value in the array
public static int Getmax (int[] arr) {
Traverse each wheel number, array length-1 (arr.length-1)
for (int i = 0; i < arr.length-1; i++) {
The number of times each adjacent two-number comparison is traversed in each round
for (int j = 0; J < Arr.length-1-i; J + +) {
Determine the size of these two numbers, the larger number is stored in a variable, swap position, and eventually the larger number of output
if (Arr[j] < arr[j+1]) {
/*int temp = arr[j];
Swap location
ARR[J] = arr[j+1];
ARR[J+1] = temp;
*/
Swap (arr,j,j+1);
}
}
}
return arr[0];

}
Gets the minimum value in the array
public static int getmin (int[] arr) {
Traverse each wheel number, array length-1 (arr.length-1)
for (int i = 0; i < arr.length-1; i++) {
The number of times each adjacent two-number comparison is traversed in each round
for (int j = 0; J < Arr.length-1-i; J + +) {
/*//define a variable to hold a smaller value
int min = 0;*/
Determine the size of these two numbers, the larger number is stored in a variable, swap position, and eventually the larger number of output
if (Arr[j] > arr[j+1]) {
/*int temp = arr[j];
Swap location
ARR[J] = arr[j+1];
ARR[J+1] = temp;*/
Swap (arr,j,j+1);

}
}
}
return arr[0];
}
Swap Location Code Extraction
public static void Swap (int[] Arr,int A,int b) {
int Temp=arr[a];
ARR[A]=ARR[B];
Arr[b]=temp;
}

}
Class choosesort{
/*
Select Sort, specify the position of the element and the following element comparison, encounter a value smaller than the specified element, swap position, until the left appears the minimum value, stop
Example: 110,10,25,57,20,-1
10,110,25,57,20,-1
10,25,110,57,20,-1
10,25,57,110,20,-1
10,25,57,20,110,-1
-1,10,25,57,20,110
*/
public static void Main (string[] args) {
Int[] arr = {110,10,25,57,20,-1};
Define a variable to accept the return value in the Getmin method
int min=getmin (arr);
System.out.println ("min=" +min);

}
public static int getmin (int[] arr) {
for (int i=0;i<arr.length-1;i++) {
Iterates over the first element compared to the following element x=i+1 represents the number of times the first element specified in the array is compared to the subsequent element in sequence
for (int x=i+1;x<arr.length;x++) {
Determines which value of the first element and subsequent elements are small after each traversal
if (Arr[i]>arr[x]) {
Define a temporary variable to hold the minimum value
int temp = Arr[i];
Swap location
ARR[I]=ARR[X];
Arr[x]=temp;

}
}
}
Return arr[0];//returns the array subscript number
}
}
Class Arroperate {
/*
Requirements: Define an array for adding and removing operations to arrays
*/
public static void Main (string[] args) {
Scanner s = new Scanner (system.in);
System.out.print ("Please enter the length of the array you want to create:");
Given the length of an array
int[] arr = new Int[s.nextint ()];
Define an accumulator that inputs several numbers
int num = 1;
Traverse the number to be entered and store it in the array
for (int i = 0; i < arr.length; i++) {
System.out.print ("Please enter" + num + "number:");
Arr[i] = S.nextint ();
num++;
}

Iterating through an array
System.out.print ("Original array is:");
for (int a:arr) {//traversal (for each) array arr, each time access to the ARR element in the array is stored in variable a

System.out.print (A + "\ t");

}
Inserts a number to the specified position
System.out.println ("\ n" + "Please enter the location of the number to insert:");
Define a variable that specifies where to hold the number
int index = S.nextint ();
System.out.print ("The number you want to insert is:");
int n = s.nextint ();
Arradd (Arr,index,n);
System.out.println ("\ n" + "Please enter this number to delete");
int x= s.nextint ();
int m=x-1;
Int[] Arras;
Arras=arradd (Arr,index,n);
Deletearray (ARR,M);

}

public static int[] Arradd (int[] arrs, int index, int n) {
Determine the length of the new array using the subscript value
if (index >= 0 && index< arrs.length) {
Expansion
int[] Newarry = arrays.copyof (Arrs, arrs.length + 1);
Newarry[index] = n;

Traversing a new array
for (int i = index + 1; i < newarry.length; i++) {
NEWARRY[I]=ARRS[I-1];
}
Arrs=newarry;
Re-traversing the new array with the original array
for (int i=0;i<newarry.length;i++) {
System.out.print (arrs[i]+ "");

}
}
return arrs;

}
public static void Deletearray (int[] Arr,int index) {
if (index>=0 && index<arr.length) {
Delete
for (int i=0;i<arr.length-1;i++) {
//
if (index<=i) {
ARR[I]=ARR[I+1];
}
}
//
Int[] c=arrays.copyof (arr,arr.length-1);
Arr=c;
for (int i=0;i<arr.length;i++) {
System.out.print (arr[i]+ "");
}

}
else {
System.out.println ("");
}
}
}

Summary of common 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.