Java Common Data algorithm _ bubble sort

Source: Internet
Author: User

There are a lot of common algorithms, and today we'll look at the bubble sort of common algorithms together.
The main core idea of bubble sorting is:
Compare adjacent two elements, if the value of the previous element is greater than the value of the next element, then the exchange, after each element of the comparison, and finally set the largest element to the last element, the loop repeats the operation, and finally formed from small to large sort.

            下面来看看我们的实例代码:            1、准备我们的数组            2、准备数组的常用操作方法            3、编写冒泡排序算法            4、测试冒泡排序算法的正确性;            package com.zcp.ch02;

/**

  • @author Administrator
  • Implementation of bubble sort
    */
    public class Bubblearray {

    Array
    Private long[] arr = null;

    The size of valid data in an array
    private int elems;
    Public Bubblearray () {
    arr = new LONG[50];
    }

    public Bubblearray (int length) {
    arr = new Long[length];
    }

    /**

      • Operations for inserting data
      • @param value
        */
        public void Insert (Long value) {
        Arr[elems] = value;
        elems++;
        }

    /**

      • Finds the specified data and returns the coordinates of the array if it is able to find it, otherwise 1.
      • @param value
      • @return
        */
        public int find (Long value) {
        int i = 0;
        for (i=0;i<elems;i++) {
        if (Value==arr[i])
        Break
        }

        if (i > (elems-1)) {
        return-1;
        }

        return i;

    }

    /***

      • Delete Data operation, Delete succeeded return true, delete failed return false
      • @param value
        */
        Public Boolean Delete (Long value) {

        int key = find (value);
        if (key = =-1) {
        System.out.println ("Failed to delete data, no data found to delete");
        return false;
        }
        for (int i = key; I < Elems; i++) {
        Arr[i] = arr[i+1];
        }
        elems--;
        return true;

    }

    /***

      • @param oldValue
      • @param newvalue
      • @return If the modification returns true successfully, the modification fails to return false
        */
        public boolean changevalue (Long Oldvalue,long newvalue) {
        int key = find (OldValue);
        if (key = =-1) {
        SYSTEM.OUT.PRINTLN ("The data that needs to be modified does not exist, the modification fails");
        return false;
        }
        Arr[key] = newvalue;
        return true;
        }

    /**

      • An algorithm for implementing bubbling Sorting
      • I in this method represents the number of comparisons of bubble sort
      • The method of J represents a bubble sort of 22 of the comparison, as for j<elems-i-1, why to reduce 1, mainly the following 22 comparison when the need j+1,
      • Otherwise, the array is out of bounds.
        */
        public void Bubblesort () {

        for (int i = 0; i < elems-1; i++) {

        for (int j = 0; j < elems-i-1; j++) {    if(arr[j]>arr[j+1]){        long tmp = arr[j];        arr[j] = arr[j+1];        arr[j+1] = tmp;    }}

        }
        }

    /**

      • The method is mainly an array of variables and displays
        */
        public void DisPlay () {
        for (int i = 0; i < Elems; i++) {
        System.out.print (arr[i]+ "");
        }
        System.out.println ();
        }

}

The test code is as follows:
Package com.zcp.ch02;

public class Testbubblemain {

public static void main(String[] args) {    BubbleArray bubbleArray = new BubbleArray();    bubbleArray.insert(10);    bubbleArray.insert(50);    bubbleArray.insert(80);    bubbleArray.insert(2);    bubbleArray.insert(5);    bubbleArray.disPlay();    bubbleArray.bubbleSort();    System.out.println("冒泡排序后的数据如下:");    bubbleArray.disPlay();}

}

Java Common Data algorithm _ bubble sort

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.