Created
June 17, 2022 13:25
-
-
Save lejonmanen/5e79c8156de1126877b4bdec01beec59 to your computer and use it in GitHub Desktop.
Binärsökning, C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Datalogi_1 | |
{ | |
public class Sort | |
{ | |
public static int existsInSortedLoopCounter; | |
public static bool ExistsInSorted(int[] array, int target) | |
{ | |
// Använd algoritmen för binärsökning: | |
// Räkna ut mittersta elementet | |
// Om vi hittade målet - returnera | |
// Finns target i vänstra eller högra halvan? | |
// Välj ut den halva som målet finns i, släng bort resten | |
// Upprepa! | |
existsInSortedLoopCounter = 0; | |
int low = 0, high = array.Length; | |
while( high > low ) | |
{ | |
existsInSortedLoopCounter++; | |
int mid = (low + high) / 2; | |
if (array[mid] == target) | |
{ | |
return true; | |
} | |
else if (array[mid] < target) | |
{ | |
low = mid + 1; | |
} | |
else | |
{ | |
high = mid - 1; | |
} | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment