Java Search Algorithm

Source: Internet
Author: User

// Binarysearch. Java
// Demonstrates recursive Binary Search
// To run this program: C> JAVA binarysearchapp
//////////////////////////////////////// ////////////////////////
Class ordarray
{
Private long [] A; // ref to array
Private int nelems; // number of data items
//-----------------------------------------------------------
Public ordarray (INT max) // Constructor
{
A = new long [Max]; // create Array
Nelems = 0;
}
//-----------------------------------------------------------
Public int size ()
{Return nelems ;}
//-----------------------------------------------------------
Public int find (long searchkey)
{
Return recfind (searchkey, 0, nElems-1 );
}
//-----------------------------------------------------------
Private int recfind (long searchkey, int lowerbound,
Int upperbound)
{
Int curin;

Curin = (lowerbound + upperbound)/2;
If (A [curin] = searchkey)
Return curin; // found it
Else if (lowerbound> upperbound)
Return nelems; // can't find it
Else // divide range
{
If (A [curin] <searchkey) // It's in upper half
Return recfind (searchkey, curin + 1, upperbound );
Else // It's in lower half
Return recfind (searchkey, lowerbound, curIn-1 );
} // End else divide range
} // End recfind ()
//-----------------------------------------------------------
Public void insert (long value) // put element into Array
{
Int J;
For (j = 0; j <nelems; j ++) // find where it goes
If (A [J]> value) // (linear search)
Break;
For (int K = nelems; k> J; k --) // move bigger ones up
A [k] = A [k-1];
A [J] = value; // insert it
Nelems ++; // increment size
} // End insert ()
//-----------------------------------------------------------
Public void display () // displays array Contents
{
For (Int J = 0; j <nelems; j ++) // for each element,
System. Out. Print (A [J] + ""); // display it
System. Out. println ("");
}
//-----------------------------------------------------------
} // End class ordarray
//////////////////////////////////////// ////////////////////////
Class binarysearchapp
{
Public static void main (string [] ARGs)
{
Int maxsize = 100; // array size
Ordarray arr; // reference to array
Arr = new ordarray (maxsize); // create the Array

Arr. insert (72); // insert items
Arr. insert (90 );
Arr. insert (45 );
Arr. insert (1, 126 );
Arr. insert (54 );
Arr. insert (99 );
Arr. insert (1, 144 );
Arr. insert (27 );
Arr. insert (1, 135 );
Arr. insert (81 );
Arr. insert (18 );
Arr. insert (1, 108 );
Arr. insert (9 );
Arr. insert (1, 117 );
Arr. insert (63 );
Arr. insert (36 );

Arr. Display (); // display Array

Int searchkey = 27; // search for item
If (ARR. Find (searchkey )! = Arr. Size ())
System. Out. println ("found" + searchkey );
Else
System. Out. println ("can't find" + searchkey );
} // End main ()
} // End class binarysearchapp
//////////////////////////////////////// ////////////////////////
// Mergesort. Java
// Demonstrates recursive merge sort
// To run this program: C> JAVA mergesortapp
//////////////////////////////////////// ////////////////////////
Class darray
{
Private long [] thearray; // ref to array thearray
Private int nelems; // number of data items
//-----------------------------------------------------------
Public darray (INT max) // Constructor
{
Thearray = new long [Max]; // create Array
Nelems = 0;
}
//-----------------------------------------------------------
Public void insert (long value) // put element into Array
{
Thearray [nelems] = value; // insert it
Nelems ++; // increment size
}
//-----------------------------------------------------------
Public void display () // displays array Contents
{
For (Int J = 0; j <nelems; j ++) // for each element,
System. Out. Print (thearray [J] + ""); // display it
System. Out. println ("");
}
//-----------------------------------------------------------
Public void mergesort () // called by main ()
{// Provides Workspace
Long [] workspace = new long [nelems];
Recmergesort (workspace, 0, nElems-1 );
}
//-----------------------------------------------------------
Private void recmergesort (long [] workspace, int lowerbound,
Int upperbound)
{
If (lowerbound = upperbound) // If range is 1,
Return; // No use sorting
Else
{// Find midpoint
Int mid = (lowerbound + upperbound)/2;
// Sort low half
Recmergesort (workspace, lowerbound, mid );
// Sort high half
Recmergesort (workspace, Mid + 1, upperbound );
// Merge them
Merge (workspace, lowerbound, Mid + 1, upperbound );
} // End else
} // End recmergesort ()
//-----------------------------------------------------------
Private void Merge (long [] workspace, int lowptr,
Int highptr, int upperbound)
{
Int J = 0; // workspace Index
Int lowerbound = lowptr;
Int mid = highPtr-1;
Int n = upperbound-lowerbound + 1; // # of items

While (lowptr <= Mid & highptr <= upperbound)
If (thearray [lowptr] <thearray [highptr])
Workspace [J ++] = thearray [lowptr ++];
Else
Workspace [J ++] = thearray [highptr ++];

While (lowptr <= mid)
Workspace [J ++] = thearray [lowptr ++];

While (highptr <= upperbound)
Workspace [J ++] = thearray [highptr ++];

For (j = 0; j <n; j ++)
Thearray [lowerbound + J] = workspace [J];
} // End Merge ()
//-----------------------------------------------------------
} // End class darray
//////////////////////////////////////// ////////////////////////
Class mergesortapp
{
Public static void main (string [] ARGs)
{
Int maxsize = 100; // array size
Darray arr; // reference to array
Arr = new darray (maxsize); // create the Array

Arr. insert (64); // insert items
Arr. insert (21 );
Arr. insert (33 );
Arr. insert (70 );
Arr. insert (12 );
Arr. insert (85 );
Arr. insert (44 );
Arr. insert (3 );
Arr. insert (99 );
Arr. insert (0 );
Arr. insert (1, 108 );
Arr. insert (36 );

Arr. Display (); // display items

Arr. mergesort (); // merge sort the Array

Arr. Display (); // display items again
} // End main ()
} // End class mergesortapp
//////////////////////////////////////// ////////////////////////
 

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.