Programming algorithm-numeric code with more than half occurrences in an array (C)

Source: Internet
Author: User

numeric code with more than half occurrences in the array (C)


This address: Http://blog.csdn.net/caroline_wendy


Title: There is a number in the array that appears more than half the length of the array, please find this number.


1. Use the high-speed sequencing (QuickSort) method to match the median (middle) with the index , output the median value, and check whether it meets the requirements.

2. Use the counting method in turn.


Code:

Method 1:

/* * main.cpp * * Created on:2014.6.12 * author:spike *//*eclipse CDT, gcc 4.8.1*/#include <stdio.h> #include    <stdlib.h>int randominrange (int min, int max) {int random = rand ()% (Max-min + 1) + min; return random;}    void Swap (int* num1, int* num2) {int temp = *NUM1;    *NUM1 = *num2; *num2 = temp;} int Partition (int data[], int length, int start, int end) {if (data = = NULL | | length <= 0 | | Start < 0 | | end    ; = length) {return-1;    } int index = Randominrange (start, end);    Swap (&data[index], &data[end]);    int small = start-1;            for (index = start; index < end; + + index) {if (Data[index] < Data[end]) {+ + small;        if (small! = index) Swap (&data[index], &data[small]);    }} + small;    Swap (&data[small], &data[end]); return small;} BOOL Checkmorethanhalf (int* numbers, int length, int number) {int times = 0;for (int i=0; i<length; ++i) {if (numbers[i] = = number) ++times;} if (times*2 <= length) {return false;} return true;}  int Morethanhalfnum (int* numbers, int length) {if (numbers = = NULL | | length <= 0) return 0;int middle = length >> 1;int start = 0;int End = Length-1;int index = Partition (numbers, length, start, end), while (Index! = middle) {if (index > middle) {//Front part of number multiple end = Index-1;index = Partition (numbers, length, start, end);} else {start = Index+1;index = part Ition (numbers, length, index+1, start);}} int result = NUMBERS[MIDDLE];IF (! Checkmorethanhalf (numbers, length, result)) return 0;return result;}    int main (void) {int str[] = {1, 2, 3, 4, 4, 4, 5, 4, 4};    int result = Morethanhalfnum (str, 9);    printf ("reuslt =%d\n", result); return 0;}


Method 2:

/* * main.cpp * *  Created on:2014.6.12 *      author:spike *//*eclipse CDT, gcc 4.8.1*/#include <stdio.h> #inclu  De <stdlib.h>bool checkmorethanhalf (int* numbers, int length, int number) {int times = 0;for (int i=0; i<length; ++i) {if (numbers[i] = = number) times++;} if (times*2 < length) return False;return true;}  int Morethanhalfnum (int* numbers, int length) {if (numbers = = NULL | | length <= 0) return 0;int result = Numbers[0];int Times = 1;for (int i=0; i<length; ++i) {if (times = = 0) {result = Numbers[i];times = 1;} if (numbers[i] = = result) Times++;else times--;} if (! Checkmorethanhalf (numbers, length, result)) return 0;return result;} int main (void) {    int str[] =  {1, 2, 3, 2, 2, 2, 5, 2, 4};    int result = Morethanhalfnum (str, 9);    printf ("reuslt =%d\n", result);    return 0;}



Output:

REUSLT = 4






Programming algorithm-numeric code with more than half occurrences in an array (C)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.