• 查找之索引查找


    c代码:

    // SearchTest.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "stdio.h"
    #include "stdlib.h"
    
    #define N 10
    #define MAX -1
    
    typedef struct dic{
        int max_number;
        int begin,end;
    }dic;
    dic list[N];
    
    //索引表创建--6为分割点,这个并没有按照每块的最大值顺序排列
    int devide(int digit[],int n)
    {
        int i=1,k,j=1,temp = n;
        int max;
        while(n>0)
        {
            k = i+5;
            printf("k="+k);printf("
    ");
            max = MAX;
            if(n<6)
            {
                printf("n<6执行");printf("
    ");
                list[j].begin = i;
                printf("begin="+list[j].begin);printf("
    ");
                for(;i<=temp;i++)
                {
                    if(digit[i]>max) 
                        max = digit[i];
                }
                list[j].end = temp;
                printf("end ="+list[j].end);printf("
    ");
            }
            else
            {
                printf("n>=6执行");printf("
    ");
                list[j].begin = i;
                printf("begin="+list[j].begin);printf("
    ");
                for(;i<=k;i++)
                {
                    if(digit[i]>max)
                        max = digit[i];
                }
                list[j].end = k;
                printf("end ="+list[j].end);printf("
    ");
            }
    
            list[j].max_number = max;
            printf("j="+j);printf("
    ");
            j++;
            printf("n="+n);printf("
    ");
            n-=6;
            
        }
        return j-1;
    }
    
    int Search(int *digit,int k,int count)
    {
        int i,pos;
        for(i=1;i<=count;i++)
        {
            if(list[i].max_number == k) return list[i].begin;
            if(list[i].max_number > k) break;//由此看来每块的最大值确实是从小到大排列过的,但是前面哪里排过呢
        }
        for(pos = list[i].begin ;pos<=list[i].end;pos++)
        {
            if(digit[pos]==k) return pos;
        }
        return -1;
    }
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int *digit;
        int pos,n,k,count;
        n=10;
        digit = (int *)malloc(sizeof(int)*(n+1));
        digit[0] = 1;
        digit[1] =2;
        digit[2] = 2;
        digit[3]= 4;
        digit[5] = 5;
        digit[6] = 6;
        digit[7] = 7;
        digit[8] = 8;
        digit[9] = 9;
        count = devide(digit,n);
        k = 4;
        if((pos = Search(digit,k,count))!=-1)
            printf("the position of the number is %d
    ",pos);
        else 
            printf("the number you want to search does not exist.
    ");
        return 0;
    }

    虽然可以正确查找,但是试图通过打印控制台分析执行步骤却不方便,还是用自己比较熟悉的java写一遍吧

    java代码:

    package sjjg;
    
    public class Dic {
    
        int max_number;
        int begin;
        int end;
        public int getMax_number() {
            return max_number;
        }
        public void setMax_number(int max_number) {
            this.max_number = max_number;
        }
        public int getBegin() {
            return begin;
        }
        public void setBegin(int begin) {
            this.begin = begin;
        }
        public int getEnd() {
            return end;
        }
        public void setEnd(int end) {
            this.end = end;
        }
        
        
    }

    以上是实体类,也就是c中的结构体,下面是算法:

    package sjjg;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class SearchTest {
        static List<Dic> list = new ArrayList<Dic>();
        
        public List<Dic> getList() {
            return list;
        }
    
        public void setList(List<Dic> list) {
            this.list = list;
        }
        
        static{
            for(int i=0;i<10;i++){
                Dic d = new Dic();
                list.add(d);
            }
        }
    
        static int devide(int[] digit,int n){
            int i=0,k,j=0,temp = n-1;
            int max;
            
            while(n>0){
                k=i+5;
                System.out.println("k="+k);
                max = -1;
                if(n<6){
                    System.out.println("n<6--");
                    list.get(j).begin = i;
                    System.out.println("begin="+i);
                    for(;i<=temp;i++){
                        if(digit[i]>max) max = digit[i];
                    }
                    list.get(j).end = temp;
                    System.out.println("end="+temp);
                }else{
                    System.out.println("n>6--");
                    list.get(j).begin = i;
                    System.out.println("begin="+i);
                    for(;i<=k;i++){
                        if(digit[i]>max) max = digit[i];
                    }
                    list.get(j).end = k;
                    System.out.println("end="+k);
                }
                
                list.get(j).max_number = max;
                j++;
                n-=6;
            }
            return j;
        }
        
        static int search(int[] digit,int k,int count){
            System.out.println("search--");
            int i,pos;
            for(i=0;i<count;i++){
                if(list.get(i).max_number == k){
                    System.out.println("list.get(i).max_number == k");
                    return list.get(i).begin;
                }
                if(list.get(i).max_number >k){
                    System.out.println("list.get(i).max_number >k");
                    break;
                }
                System.out.println("循环次数i="+i);
            }
            System.out.println("begin to for(pos = list.get(i).begin;pos<=list.get(i).end;pos++)");
            for(pos = list.get(i).begin;pos<=list.get(i).end;pos++){
                System.out.println("pos="+pos+"digit[pos]="+digit[pos]);
                if(digit[pos] == k) return pos;
            }
            return -1;
        }
        
        
        
        public static void main(String[] args) {
            int digit[] = new int[10];
            digit[0] = 1;
            digit[1] =2;
            digit[2] = 2;
            digit[3]= 4;
            digit[5] = 5;
            digit[6] = 6;
            digit[7] = 7;
            digit[8] = 8;
            digit[9] = 9;
            int pos,n,k,count;
            n=10;
            count = devide(digit,n);
            System.out.println("count="+count);
            k = 4;
            if((pos = search(digit,k,count))!=-1){
                System.out.println("pos="+pos);
            }else{
                System.out.println("not exit");
            }
            
        }
    }

    索引查找是在简单查找的基础上分块,所以又叫分块查找。

    未解决的问题:怎么将每个块按照最大值的大小顺序排列,具体的算法分析,等以后会补上。

  • 相关阅读:
    设计模式之单例模式实践
    有关集合的foreach循环里的add/remove
    项目中常用的MySQL优化方法--壹拾玖条
    Solr
    Lucene补充
    Lucene
    一千行 MySQL 学习笔记
    Servlet
    CSS未知宽高元素水平垂直居中
    深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/waiwai4701/p/4302774.html
Copyright © 2020-2023  润新知