Several common algorithms in Java

Source: Internet
Author: User

1) Bubble Sort:

Compares the two adjacent elements in turn, placing the largest (or smallest) element in an unordered sequence at the end of an unordered sequence, in a single comparison.



  1. Public class Bubblesort {
  2. public static void sort (int data[]) {
  3. For (int i = 0; i < data.length-1; i++) {
  4. For (int j = 0; J < data.length-i- 1; j + +) {
  5. if (Data[j] > data[j + 1]) {
  6. int temp = data[j];
  7. DATA[J] = data[j + 1];
  8. Data[j + 1] = temp;
  9. }
  10. }
  11. }
  12. }
  13. }


2) Select Sort:

Each time a minimum (or maximum) element is selected from the data element to be sorted, the order is placed at the end of the ordered sequence until all the data elements to be sorted are exhausted.

  1. Public class Selectionsort {
  2. public static void sort (int data[]) {
  3. int minval;
  4. int minindex;
  5. For (int i = 0; i < data.length- 1; i++) {
  6. Minval = Data[i];
  7. Minindex = i;
  8. For (int j = i + 1; j < Data.length; J + +) {
  9. if (Data[j] < minval) {
  10. Minval = Data[j];
  11. Minindex = j;
  12. }
  13. }
  14. if (minval! = data[i] && minindex! = i) {
  15. Data[minindex] = Data[i];
  16. Data[i] = minval;
  17. }
  18. }
  19. }
  20. }


3) Insert Sort:

The sequence is divided into two parts ordered and unordered, and each processing is to compare the first element of the unordered sequence with the elements of the ordered series, to find the insertion position and insert the element into the proper position of the ordered sequence.

 
  1. Public class Insertionsort {
  2. public static void sort (int data[]) {
  3. For (int i = 1; i < data.length; i++) {
  4. For (int j = i; j > 0; j--) {
  5. if (Data[j] < data[j- 1]) {
  6. int temp = data[j];
  7. DATA[J] = data[j- 1];
  8. Data[j- 1] = temp;
  9. }
  10. }
  11. }
  12. }
  13. }


4) Merge Sort:

The two (or more than two) ordered tables are combined into a new ordered table, that is, the ordered sequence is divided into several sub-sequences, each of which is ordered. Then the ordered subsequence is combined into a whole ordered sequence. The sorting process is as follows:
(1) The application space, the size of which is the sum of two sorted sequences, which is used to store the combined sequence
(2) Set two pointers, the initial position is the starting position of two sorted series
(3) Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position
(4) Repeat step 3 until a pointer reaches the end of the sequence
(5) Copy all remaining elements of another sequence directly to the end of the merge sequence

 
  1. Public class MergeSort {
  2. public static void sort (int data[], int start, int end) {
  3. if (Start < end) {
  4. int mid = (start + end)/ 2;
  5. Sort (data, start, mid);
  6. Sort (data, Mid + 1, end);
  7. Merge (data, start, mid, end);
  8. }
  9. }
  10. public static void merge (int data[], int start, int mid, int end) {
  11. int temp[] = new Int[end-start + 1];
  12. int i = start;
  13. Int J = mid + 1;
  14. int k = 0;
  15. While (I <= mid && J <= end) {
  16. if (Data[i] < data[j]) {
  17. temp[k++] = data[i++];
  18. } Else {
  19. temp[k++] = data[j++];
  20. }
  21. }
  22. While (I <= mid) {
  23. temp[k++] = data[i++];
  24. }
  25. While (j <= end) {
  26. temp[k++] = data[j++];
  27. }
  28. For (k = 0, i = start; k < temp.length; k++, i++) {
  29. Data[i] = temp[k];
  30. }
  31. }
  32. }


5) Quick Sort:

By dividing the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the whole sorting process can be recursive, in order to achieve the entire data into an ordered sequence.

 
    1. Public class QuickSort {
    2. public static void sort (int data[], int start, int end) {
    3. if (End-start <= 0) {
    4. return;
    5. }
    6. int last = start;
    7. For (int i = start + 1; I <= end; i++) {
    8. if (Data[i] < Data[start]) {
    9. int temp = Data[++last];
    10. Data[last] = Data[i];
    11. Data[i] = temp;
    12. }
    13. }
    14. int temp = Data[last];
    15. Data[last] = Data[start];
    16. Data[start] = temp;
    17. Sort (data, start, last- 1);
    18. Sort (data, last + 1, end);
    19. }
    20. }

Several common algorithms in Java

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.