473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sorted and shifted array

I found the following question, and couldn't get the best answer:

if you have a sorted and shifted array such like {5,6,7,1,2,3,4}, how
to find one element "2"?

Thanks.
Vol
Jun 1 '08 #1
4 1969
On 2008-05-31 23:03:13 -0400, Vols <vo********@gmail.comsaid:
I found the following question, and couldn't get the best answer:

if you have a sorted and shifted array such like {5,6,7,1,2,3,4}, how
to find one element "2"?
It's the fourth element. Beyond that, the problem is seriously underspecified.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 1 '08 #2

"Vols" <vo********@gmail.coma écrit dans le message de news:
d8**********************************...oglegroups.com...
>I found the following question, and couldn't get the best answer:

if you have a sorted and shifted array such like {5,6,7,1,2,3,4}, how
to find one element "2"?

Thanks.
Vol
Check out algorithms like

std::find, std::find_if

int arr[5] = {1,2,3,4,5}
int* it = std::find(arr, arr+5, 2); // it now point to arr[1]

and you can use std::distance to obtain the position of the element
Jun 1 '08 #3
On Jun 1, 11:03*am, Vols <volunte...@gmail.comwrote:
I found the following question, and couldn't get the best answer:

if you have a sorted and shifted array *such like {5,6,7,1,2,3,4}, how
to find one element "2"?

Thanks.
Vol
the sorted and shifted array contains 2 sorted subsequence
first, find the 2 subsequence
then check which subsequence is the element in, and do binary search
Jun 2 '08 #4
Vols wrote:
I found the following question, and couldn't get the best answer:

if you have a sorted and shifted array such like {5,6,7,1,2,3,4}, how
to find one element "2"?
Your requirements are not clear. Do you need to find the element that
contains the value 2, or the element that was orignally the 2nd element
before the shift?

--
Jim Langston
ta*******@rocketmail.com
Jun 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Hill | last post by:
I have this large array with dates in it. There is a function that sorts it for me. The only problem is that in order for it to get sorted properly I have to sort it AlphaNumerically first using ...
10
by: Ryan Graham | last post by:
I totally bombed this question in an interview so I'm posting my answer here for comments and suggestions... perhaps (god help me) I'm just not that bright, but this works and seems to be fairly...
2
by: Murt | last post by:
if i perform a sort on an array, how do i get the sorted array to be displayed in a listbox? thanks murt
8
by: Rajesh | last post by:
Question :An element in a sorted list can be found in O(log n) time via binary search. But suppose I rotate the sorted list at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
7
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, Is it possible to delete an element from a sorted array with O(1) time? Best regards
5
by: Vols | last post by:
I found the following question, and couldn't get the best answer: if you have a sorted and shifted array such like {5,6,7,1,2,3,4}, how to find one element "2"? Thanks. Vol
1
by: Harold Howe | last post by:
Howdy all, The msdn help says this about SorteList<k,v>: "If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary." My question is this: how do I...
16
by: Jon Harrop | last post by:
I need a data structure with the following capabilities: 1. Fast access to the smallest element. 2. Fast insertion of a new element. 3. Fast deletion of an existing element. I have been...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.