Package Suanfa;public class Maopao {public static void main (string[] args) {//TODO auto-generated method stubint[] A = { 3, 2, 1};int temp;int n = a.length;//Compare n-1 big round for (int i = 0; i "Java algorithm" bubble sort
Time complexity O (N2), requires a n*n comparison, a number of positions per n timesImportJava.util.Scanner; Public classBubblesort { Public Static voidMain (string[] args) {int[] arr =New int[10]; Scanner Array=NewScanner (system.in); for(inti = 0;i) Arr[i]=Array.nextint (); for(inti=0;i) for(intj=0;j){ if(arr[j]>arr[j+1]){ intTemp=arr[j+1]; Arr[j+1]=Arr[j]; ARR[J]=temp; } } for(inti=0;i) System.out.print (Arr[i]+" "); System.ou
Defines an array of type int and arranges the number of them from small to large.
Train of thought: Take two numbers from the array to compare and keep the smallest value to compare with the other values in the group, get the minimum value, and put him first (I loop once, J makes all the comparisons of all values)
The first for loop can only complete the arrangement of a minimum value at a time, and when the loop completes, the sort is completed.
P
first, bubble sort
Code implementation:
public class Test_bubblesort {public
static void Main (string[] args) {
int[] arr = {23,56,15,13,36};
Bubblesort (arr);
Print (arr);
}
Bubble sort public
static void Bubblesort (int[] arr) {for
(int i = 0;i
The time complexity of the algorithm is O (n^2), and th
first, the basic idea:
Bubble sort is a simple sort of commutative class. The basic idea is to scan the elements that are to be sorted from scratch, to compare the adjacent elements in sequence during the scan, and to move the elements of the key value back. After each trip, the element with the largest key value is moved to the end, and the position of the elem
Import java.util.arrays;//=================================================//File name:bubble_sort//------------ ------------------------------------------------------------------//author:common//class Name: arrays_bubble//property://Method: C Lass arrays_bubble{private int[] arrays;private int curnum;public arrays_bubble (int max) {//Create an empty array of Max lengths super (); arrays = new Int[max];curnum = 0;} public void Insert (int value) {//empty array add element arrays[curnum] = value;
Bubble Sort:is to compare the next two elements by index, if greater than/less than (depending on the need to ascending or descending order), then displace, otherwise do not changeSuch a round down, compared n-1 times, n equals the number of elements; n-2, n-3 ... Until the last round, compared 1 timesSo the number of comparisons is decremented: from n-1 to 1Then the total number of comparisons is: 1+2+3+...+ (n-1), calculated in a linear formula: (1+
Sort: bubble sort vs Quick Sort, bubble sort
In the process of development, we often encounter set sorting. In general, we use list. the OrderBy () method does not need to be concerned with the implementation of the algorithm. in
The relationship between 8 sorts:
1, direct insertion sort
(1) Basic idea: In the set of numbers to be sorted, suppose that the front (n-1) [n>=2] number is already a row
In a good order, now you have to insert the nth number into the preceding ordered number so that the number of n
It's a good order, too. Repeat the loop until all the order is sorted.
(2) Example
(3) Implement with Java[Plain] View plai
Public classMaopao { Public Static voidMain (string[] args) {int[] Array = {332,94,65,4,15,64,16,5,4}; inttemp = 0 ; /*** I:0-8 9 * First time: I=0 the inner for should loop 8 times so the number is **/ for(intI =0; i){ for(intj = 0; J ) { if(array[j]>array[j+1]) {temp=Array[j]; ARRAY[J]= Array[j+1]; Array[j+1] =temp; } } } for(intK:array) {System.out.println (k); } }}I directly use the algorit
Algorithm Core1 Public voidBubblesort () {2 for(inti=0;i//a couple of times .3 for(intj=0;j//in each trip, the last I was in an orderly state.4 if(a[j]>a[j+1]){5Swap (j,j+1);6 }7 }8 }9}Complete algorithm1 PackageBubblesort;2 3 Public classSort {4 Public Static voidMain (String args[]) {5Array arr =NewArray (10);6 for(inti=0;i){7Arr.insert (10-i);8 }9 Arr.bubblesort ();Ten Arr.display (); One }
Insert sort + bubble sort + select sort, insert sort bubble select
When inserting the sort work mechanism and playing cards, sorting in the hand is similar. When we started to touch th
1. Compare and sort bubble sort, and sort bubble
Bubble Sorting is one of the most entry-level algorithms in sorting algorithms. It is easy to understand and often serves as an entry-level Algorithm for sorting in the classroom.
Basic idea:In the set of numbers to be sorted, the total number in the range that is not currently in sequence, the top-down pairs of adjacent two numbers are compared and adjusted sequentially, so that the larger number to sink , smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged. Algorithm implementation: (HDU 1040 pro-Test AC)#include using namespacestd;Const intN =1005;voidBubblesort (intA[],int );
The process of bubbling sorting is to first compare the keywords of the first record with the keywords of the second record, and, in reverse order, Exchange two records and then compare the keywords for the second record and the third record. And so on, until the n-1 record and the nth record's keywords have been compared. The above process is called the first bubbling sort, then the second trip to the previous n-1 the same operation, ...The quick
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.