Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with insertion,

Status
Not open for further replies.

Ethereal

Programmer
Sep 12, 2005
2
US
I need help writing the code for this. I was thinking along the lines on how a binary search works, but instead of looking for a value you look for a location and insert the integer there and maintain the sorted order.

This is my algorithm:

Description: inserts x into the list and maintains the order
Example:
{}, {50}, {25,50}, {25,36,50}, {25,36,50,76},
{25,36,50,76}
Algorithm0:
lower = 0
upper = n
loop while lower < upper
mid = (upper+lower)/2
if x >= a[mid] then lower = mid+1, loop
if x < a[mid] then upper = mid-1, loop
//if x = a[mid] then search to right until different value found

start at mid
if x<a[mid] then mid is the place
if x>a[mid] then while (x >a[mid))
mid++

mid is the point

--------------------------------
bool COOL::insertBinary(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top