Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Sort search algorithm Java - application examples with recursive dichotomy     - RHEL5.x RHEL6.x replace CentOS yum source (Linux)

- Android LayoutInflater source parsing (Programming)

- Object-C in the preparation of multi-parameter function parameter is omitted (Programming)

- MySQLbinlog combat on using data recovery (Database)

- Python objects (Programming)

- How to install Nginx on FreeBSD 10.2 as an Apache reverse proxy (Server)

- SUSE Firewall Configuration (Linux)

- MySQL Tutorial: Building MySQL Cluster under Linux (Database)

- Effective Java - lazy initialization (Programming)

- How to deploy Icinga client (Server)

- MongoDB configuration in Ubuntu 14.04 (Database)

- Ubuntu manually set the DSL broadband connection (Linux)

- Use XtraBackup be physical standby database MySQL (Database)

- The Oracle VM VirtualBox use U disk under ubuntu (Linux)

- How to install Docker and basic usage on Ubuntu 15.04 (Server)

- ASM required disk format process in Solaris platforms (Linux)

- Towards Docker (Server)

- About Java 7 module system (Programming)

- Debian (Wheezy) Install Redmine 2.6 (Programming)

- To install Samba server on CentOS 6.6 (Server)

 
         
  Sort search algorithm Java - application examples with recursive dichotomy
     
  Add Date : 2017-07-22      
         
         
         
  Problem Description:

To find and sort the array elements, and the use of recursive dichotomy.

Complete Example 1:

public class SortDemo
{
  public static void main (String [] args)
  {
      int [] arr = {10,2,300,41,15,6};
      for (int a: arr)
      {
        System.out.print ( "[" + a + "]");
      }
    // New SortDemo () insertSort (arr).;
    new SortDemo () binaryInsertSort (arr).;
      System.out.println ();
      for (int a: arr)
      {
        System.out.print ( "[" + a + "]");
      }
  }
    
  // Sort dichotomy
  public void binaryInsertSort (int [] a)
  {
      for (int i = 1; i < a.length; i ++)
      {
        int temp = a [i];
        int low = 0;
        int high = i-1;
        while (low < = high)
        {
            int mid = (low + high) / 2;
            if (a [mid] < temp)
            {
                low = mid + 1;
            }
            else
            {
                high = mid-1;
            }
        }
        for (int j = i-1; j> = low; j--)
        {
            a [j + 1] = a [j];
        }
        a [low] = temp;
      }
  }
    
  // Insertion sort
  public void insertSort (int [] a)
  {
      Coordinate preceding element // participate compare elements; int i
      // Current element involved in the comparison; int key
      for (int j = 1; j < a.length; j ++)
      {
        key = a [j];
        i = j-1;
        while (i> = 0 && a [i]> key)
        {
            a [i + 1] = a [i]; // right one
            i -; // i advance
        }
        a [i + 1] = key;
      }
  }
}

Complete Example 2:

// Binary search
import java.util *.;
public class BinarySearchDemo
{
  public static void main (String [] args)
  {
        int [] arr = {10,2,300,41,15,6};
        Arrays.sort (arr); // sort the array of natural
        . //System.out.println(new BinarySearchDemo () method1 (arr, 41));
        System.out.println (new BinarySearchDemo () method2 (arr.length-1,0, arr, 41).);
  }
  // Recursive binary search
  public String method2 (int b, int s, int [] a, int g)
  {
      int m; // Find the coordinates of the median part
      if (s < b)
      {
        m = (b + s) / 2;
        if (a [m] == g)
        {
            return "you are looking for [" + g + "] is ranked in the array size [" + (m + 1) + "] position.";
        }
        else if (a [m]> g) // if the intermediate value is greater than the target value, comparing the left half,
        {
            b = m-1; // first part of the highest-coordinate values ​​into the middle to find a
            return method2 (b, s, a, g);
        }
                  else // If the intermediate value is smaller than the target value, then the right half of the comparison
        {
              s = m + 1; // Find the lowest position after part becomes a middle value
              return method2 (b, s, a, g);
        }
      }
      return "you are looking for [" + g + "] is not within the array.";
  }
    
  // Parameter is an array and the target value
  public String method1 (int [] a, int g)
  {
      Arrays.sort (a); // sort the array of natural
      int b = a.length-1; // Find the highest part of Coordinates
      int s = 0; // Find the lowest part of Coordinates
      int m; // Find the coordinates of the median part
      while (b> = s)
      {
        m = (b + s) / 2;
        if (a [m] == g)
        {
              
            return "you are looking for [" + g + "] is ranked in the array size [" + (m + 1) + "] position.";
        }
        else if (a [m]> g) // if the intermediate value is greater than the target value, comparing the left half,
        {
            b = m-1; // first part of the highest-coordinate values ​​into the middle to find a
        }
        else // If the intermediate value is smaller than the target value, then the right half of the comparison
        {
              s = m + 1; // Find the lowest position after part becomes a middle value
        }
      }
      return "you are looking for [" + g + "] is not within the array.";
  }
    
}
     
         
         
         
  More:      
 
- Let your PHP 7 faster (GCC PGO) (Linux)
- Oracle () trunc function usage (Database)
- C # mobile side and PC-side data exchange (Database)
- Python, and / or (Programming)
- Spring Data JPA call a stored procedure examples (Programming)
- Linux disk quota-related instruction (Linux)
- In Spring AOP example explanation (Programming)
- Linux System Getting Started Learning: Linux in the last command (Linux)
- DELL D630 Wireless LAN Driver Installation CentOS6 (Linux)
- Oracle set the timer task JOB scheduler to execute stored procedures or PL / SQL code block (Database)
- Linux server network penetration testing (Linux)
- MySQL server after an unexpected power outage can not start (Database)
- Ruby and Python simple comparison (Programming)
- A key installation Gitlab 7 on RHEL6.4 and Setup Mail TX (Linux)
- RedHat 6.5 installation and deployment Openfire (Server)
- Linux network security probing tools Hping2 (Linux)
- Observation network performance tools for Linux (Linux)
- Java memory model subclasses (Programming)
- Arduino UNO simulation development environment set up and run simulation (Linux)
- To learn from scratch OpenWrt perfect tutorial (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.