c# - Binary Search String List

有些话、适合烂在心里 提交于 2019-12-13 07:13:46

问题


I'm currently building a console application that can search and sort different types of data. As the question suggests; I need help making a binary search algorithm for a string list. I must point out that I can't make use of the built-in c# functions like list.binarysearch , the algorithm has to be coded from scratch. Also the list contains multiple items of the same string value so I need to find all of these too. I've already sorted the list in alphabetical order so I will make use of algorthims to find the upper and lower-bounds of the string value being search.

I will start off by showing how I've formatted the list.

var list = new List<Demo>
        {   
            new Demo {Col = "Blue", S1 ="88", S2 ="Yes"},
            new Demo {Col = "Green", S1 ="43", S2 ="Yes"},
            new Demo {Col = "Green", S1 ="216", S2 ="No"},
            new Demo {Col = "Yellow", S1 ="100", S2 ="No"}
        };

The list has been sorted in to alphabetical order of the 'Col' values. The list is actually much larger than this but you get the idea. The binary search needs to search all the 'Col' values and find a particular string value.

Below I will show my binary search algorithm that works on int arrays.

public static int BinarySearch_R(int key, int[] array, int low, int high)
    {
        if (low > high) return -1;
        int mid = (low + high) / 2;
        if (key == array[mid])
        {

            return mid;
        }
        if (key < array[mid]) {
            return BinarySearch_R(key, array, low, mid - 1);
        } else {
            return BinarySearch_R(key, array, mid + 1, high);
        }
    }

I want to adapt this algorithm for my list above.

Finally I will show you the code for searching the upper and lower bounds.

public static int lower_bound(int key, int[] arr , int low, int high)
    {
        if (low > high)
            //return -1;
            return low;

        int mid = low + ((high - low) >> 1);
        //if (arr[mid] == key) return mid;

        //Attention here, we go left for lower_bound when meeting equal values
        if (arr[mid] >= key) 
            return lower_bound(key, arr, low, mid - 1);
        else
            return lower_bound(key, arr, mid + 1, high);
    }

public static int upper_bound(int key, int[] arr,  int low, int high)
    {
        if (low > high)
            //return -1;
            return low;

        int mid = low + ((high - low) >> 1);
        //if (arr[mid] == key) return mid;

        //Attention here, we go right for upper_bound when meeting equal values
        if (arr[mid] > key) 
            return upper_bound(key, arr, low, mid - 1);
        else
            return upper_bound(key, arr, mid + 1, high);
    }

I know that I've asked for a lot to be done so even if you just help out part of the problem I'll be thankful. If you need any more information or code, just ask.


回答1:


First of all I would like to point out, you are passing an array into your function. So, why are you using the list?

Again, as far as I know, Binary Search works only when an array is sorted. So, I would suggest, sort your list beforehand. Though I cannot find any solution for searching your custom list(using Binary search) even after sorting.

I hope these points will help you. May be, you know them already. I am new at StackOverflow, so, please let me know whether my answer helped you or not.



来源:https://stackoverflow.com/questions/36663070/c-sharp-binary-search-string-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!