Загрузка страницы

Find an element in a sorted rotated array without finding pivot (minimum element)

Problem:
Given a sorted integer array with distinct elements which is rotated any number of times and an integer num, find the index of num in the array.
If not found, return -1.

Solution:
1: Initialize start = 0, end = array length - 1
2: Repeat following steps till start less than or equal to end:
(a) Set mid = (start + end)/2
(b) If num == array[mid], return mid.
(c) If array[start…mid] is sorted, i.e. array[start] is less than array[mid]
(i) If array[start] is less than or equal to num is less than or equal to array[mid], set end = mid-1
(ii) Else set start = mid+1
(d) Else If array[mid…end] is sorted, i.e. array[mid] less than or equal to array[end]
(i) If array[mid] is less than or equal to num is less than or equal to array[end], set start = mid+1
(ii) Else set end = mid-1
3: If not found, return -1.

Related Videos:

1: Find an element in a sorted rotated array: https://www.youtube.com/watch?v=6pSzsJH56BA&index=3&list=PLamzFoFxwoNhR2uFoqm6nr8VgsERgTmYy

2: Binary Search Playlist: https://www.youtube.com/playlist?list=PLamzFoFxwoNhR2uFoqm6nr8VgsERgTmYy

Code and Algorithm Visualization: http://www.ideserve.co.in/learn/find-an-element-in-a-sorted-rotated-array-without-finding-pivot

Website: http://www.ideserve.co.in

Facebook: https://www.facebook.com/IDeserve.co.in

Видео Find an element in a sorted rotated array without finding pivot (minimum element) канала IDeserve
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
29 августа 2015 г. 20:52:45
00:03:25
Яндекс.Метрика