Uniqueness verification element (2 cyclic optimization method and the fast row)

School exercise, I simply do not impress, it is worth finishing, I saved it

Problem 4

 

1.1. Uniqueness verification element (2 cyclic)

 

1.1.1. Algorithm Description

 

 Checking the unique element, the main methods are: establishing double loop, each checksum and other elements of

 

1.1.2. Pseudo code

 

UniqueElements(A[0..m-1])

// verify that a given array of elements is unique

// Input : Array A [0..n-1]

// output : If the A elements all unique, returns true

// otherwise returns false

for i<- 0 to n-2 do

  for j<- i+1 to n-1 do

    if A[i]=a[j] return false

return true

 

1.1.3. Algorithm

 

bool UniqueElements(int ele[],int len){

    for(int i=0;i<len-1;i++)

        for(int j=i+1;j<len;j++)

            if (it [i] == he [j]) return false;

    return true;

}

 

2.1. Optimization algorithm (quick sort optimization)

 

2.1.1. Algorithm Description

 

Use quick sort sorting algorithm to optimize the main methods are: use of quick sort algorithm for sorting the elements, and then take a first value.

 

2.1.2 verifies the unique elements (quick sort optimization, pseudocode )

 

UniqueElements(A[0..m-1]

// Verify that the given elements in the array is unique

// Input : Array A [0..n-1]

// output : If the A elements all unique, returns true

// otherwise returns false

qsort(A)

for i<-0 to len-1 do

  if A[i]=a[i+1] return false

return true

 

2.1.3 Improved algorithm

 

int cmp(const void *a,const void *b){

    return *(int*)b-*(int*)a;

}

bool UniqueElements(int ele[],int len){

    qsort (ele, len, sizeof ( int), cmp); // this qsort is <stdlib.h> comes with the function

    for(int i=0;i<len-1;i++)

        if (it [i] == he [i + 1]) return false;

    return true;

}

 

3.1. Fast row algorithm (quick sort)

 

3.1.1. Algorithm Description

 

Fast row algorithm, is an improvement on the bubble sort, in which, with the rule of thought points. We take a Base , followed by about recursive sort. In the sorting process, we have right to left, just to find than the base small numbers, the value assigned to the left and right value, and then from left to right, just to find than the base large numbers, will be left value to the right value, the last two values are equal, we will base to left value.

 

3.1.2. Pseudo code

 

   get_base(ele[0,m-1],left,right)

// obtain the target partition

// Input : an element, given a value as left and right value range

// output : returns the index divided

base<-ele[left]

while left<right do

  while left<right and ele[right] >= base do

    right--

  end

  while left<right and ele[left] <= base do

    left++

  end

  ele[left]=base

end

return left;

 

quick_sort(ele[0,m-1],left,right)

// recursive sort

// Input : an element, given a value as left and right value range

if left<right do

  index<-getbase(ele,left,right)

  quick_sort(ele,left,right)

  quick_sort(ele,left,right)

end

 

 

3.1.3. Realization

 int get_base(int ele[],int left,int right){

    int base=ele[left];

    while(left<right){

        while(left<right && ele[right]>=base) right--;

        he [left] = it [right];

        while(left<right && ele[right]<=base) left++;

        it [right] = it [left];

    }

    ele[left]=base;

    return left;

}

void quick_sort(int ele[],int left,int right){

    if(left<right){

        int index=get_base(ele,left,right);

        quick_sort(ele,0,index-1);

        quick_sort(ele,index+1,right);

    }

}

4.1. Test Summary

 

When verifying the unique elements of our usual practice is to perform double loop, two-Comparing the two, if any two the same, it returns false , otherwise return to true , this algorithm, the complexity of time is O (n ^ 2) , space complexity is O (1) , so we thought of optimization, the use of fast row, optimize, and then in turn be traversed, can be obtained. We think about the use of fast row, our time complexity is O (nlogn) , space complexity is O (1) . This efficiency is significantly optimize the efficiency much more than before.

 

Quick drain algorithm, we can use the C language <stdlib.h> comes with qsort function, the function has the following prototype, void qsort (void * buf, size_t NUM, size_t size, int (* Compare) (const void * , const void *)); first buf , refers to your array, the second parameter size_t refers to an unsigned integer, the length of your array, the third parameter is the size of your array elements, the fourth parameter is a void pointer, we have C learning program, know, void pointer can point to any, can be arbitrary points, so we defined comparison function cmp time, pay attention to, it is defined const void * parameter types, then a strong after Switch element pointer type, compared to do so, can directly discharge the fast algorithm for the entire C arbitrary order program.

 

In handwritten quick sort, we should note that moving around on value, should first move the right value assigned to the left, then move left, assigned to the right, finally, return to the lower left mark, as the division point.

We understand the division of time, can not forget a thought, the idea of ​​the rule of division, this idea in the future we will always be able to get by.

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11515537.html