bSearch java程序

时间:2016-10-06 01:59:30

标签: java bsearch

我不知道我的代码有什么问题。我应该输入一个输入文件,并将数字从最低到最大排序。当我运行程序时,它返回:在insertInOrder之后从P4input.txt中排序25个整数的数组: 31 5 8 8 19 23 25 27 27 31 69 70 71 75 90 98 103 103 109 140 145 145 153 157 162

我不知道为什么第一个号码乱了。

/* Project4.java  InsertInOrder with bSearch optimization to compute insertion     index */

import java.util.*;
import java.io.*;

public class Project4
{
static final int INITIAL_CAPACITY = 5;

public static void main( String args[] ) throws Exception
{
    // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
    if (args.length < 1 )
    {
        System.out.println("\nusage: C:\\> java Project4 <input filename>\n\n"); // i.e. C:\> java Project4 P4input.txt
        System.exit(0);
    }

    // LOAD FILE INTO ARR USING INSERINORDER

    Scanner infile =  new Scanner( new File( args[0] ) );
    int[] arr = new int[INITIAL_CAPACITY];
    int count= 0;
    while (infile.hasNextInt())
    {
        if ( count==arr.length )
                arr = upSizeArr(arr);
        insertInOrder( arr, count++, infile.nextInt() );
    }
    infile.close();
    arr=trimArr(arr,count); // Now count == .length
    System.out.println( "Sorted array of " + arr.length + " ints from " + args[0] + " after insertInOrder:" );
    printArray( arr );  // we trimmed it thus count == length so we don't bother to pass in count

} // END MAIN
// ############################################################################################################

// USE AS IS - DO NOT MODIFY
static void printArray( int[] arr  )
{
    for( int i=0 ; i<arr.length ;++i )
        System.out.print(arr[i] + " " );
    System.out.println();
}

// USE AS IS - DO NOT MODIFY
static int[] upSizeArr( int[] fullArr )
{
    int[] upSizedArr = new int[ fullArr.length * 2 ];
    for ( int i=0; i<fullArr.length ; ++i )
        upSizedArr[i] = fullArr[i];
    return upSizedArr;
}

// USE AS IS - DO NOT MODIFY
static int[] trimArr( int[] oldArr, int count )
{
    int[] trimmedArr = new int[ count ];
    for ( int i=0; i<count ; ++i )
        trimmedArr[i] = oldArr[i];
    return trimmedArr;
}

// ############################################################################################################
static void insertInOrder( int[] arr, int count, int key   )
{
    int index=bSearch( arr, count, key ); // LEAVE THIS HERE
    if (arr[arr.length - 1] == 0)
    {
    for (int i = count; i >= index + 1; i--) 
    {
    arr[i] = arr[i - 1];
    }
    arr[index]=key; // LEAVE THIS HERE
}
}
static int bSearch(int[] a, int count, int key)
{
    int hi = count-1;
    int lo = 0;
    int mid = 0;

    if(hi == -1) 
    {
        return lo;
     } 
     else
     {
     mid = (hi+lo)/2;
     }
    while (lo <= hi) 
    {
    if (key==a[mid])
    {
        return (mid+1);
    }
    else if (key < a[mid])
    {
        hi = mid-1;
        mid = (hi+lo)/2;
    }
    else 
    {
        lo = mid +1;
        mid = (hi+lo)/2;
    }
}
    return (mid +1);
}       

} // END PROJECT4

0 个答案:

没有答案