天天看點

combine the random array + quick sort + get mode

// test the new built  data structure could defined out of the for iteration but also be changed with its variable in it

// this code could be divided into three parts, part 1:create an array randomly, part 2: make the array in order by quick sort

// part 3, using the get_mode function to get the mode in the array

// the disadvantage of the get_mode function is that if the mode in the array is not single, we cannot get them all 

The results:

The original array:
1 9 5 5 1 6 8 10 4 5 1 5 6 2 1 4 7 9 6 10
The sorted array:
1 1 1 1 2 4 4 5 5 5 5 6 6 6 7 8 9 9 10 10
The mode is 1 and its frequency is: 4
           

The codes:

// test the new built  data structure could defined out of the for iteration but also be changed with its variable in it
// this code could be divided into three parts, part 1:create an array randomly, part 2: make the array in order by quick sort
// part 3, using the get_mode function to get the mode in the array
// the disadvantage of the get_mode function is that if the mode in the array is not single, we cannot get them all
#include <iostream>
#include <ctime>
#include <cstdlib>
#define MAXN 20;
void create_array(int a[], int n);
void dis_array(int a[], int n);
void quick_sort(int a[], int low, int high);
int partition_quick_sort(int a[], int low, int high);
struct val_fre{
    int data;
    int freq;
};
val_fre *get_mode(int a[], int n);
using namespace std;
int main()
{
    srand((unsigned)time(NULL));
    int n = MAXN;
    int a[n];
    create_array(a, n);
    cout << "The original array: " <<endl;
    dis_array(a, n);
    quick_sort(a, 0, n-1);
    cout <<endl << "The sorted array: " <<endl;
    dis_array(a, n);
    cout <<endl;
    val_fre *result;
    result = get_mode(a, n);
    cout << "The mode is "<< result->data << " and its frequency is: "<< result->freq;
}
void create_array(int a[], int n)
{
    for(int i = 0; i<n; ++i){
        a[i] = rand()%10+1;
    }
}
void dis_array(int a[], int n)
{
    for(int i=0; i<n; ++i){
        cout << a[i] << " " ;
    }
}
void quick_sort(int a[], int low, int high)
{
    int pivot;
    if(low >= high){
        return ;
    }else{
        pivot = partition_quick_sort(a, low, high);
    }
    quick_sort(a, low, pivot-1);
    quick_sort(a, pivot+1, high);
}
int partition_quick_sort(int a[], int low, int high)
{
    int temp_val = a[low];
    while(low < high){
        while(low < high && a[high]>= temp_val){
            --high;
        }
        a[low] = a[high];
        while(low < high && a[low] <= temp_val){
            ++low;
        }
        a[high] = a[low];
    }
    if(high == low){
        a[low] = temp_val;
    }
    return low;
}
val_fre *get_mode(int a[], int n)
{
   val_fre *max_mode, *temp_mode;
        max_mode = new val_fre;
        max_mode->data = 0;
        max_mode->freq = 0;
        temp_mode = new val_fre;
        temp_mode->data = a[0];   //初始值不能夠在這裡
        temp_mode->freq = 0;
        for(int i = 0; i < n; ){      //這裡面對于i的改變要由下面的條件來判斷
            if(temp_mode->data == a[i] ){
                temp_mode->freq += 1;
                ++i;
            }else{
                if(max_mode->freq < temp_mode->freq){
                    max_mode->data = temp_mode->data;
                    max_mode->freq = temp_mode->freq;
                }
                temp_mode->data = a[i];  //這個初始化的條件,隻要在前後兩個數字不一緻的時候就要進行初始化
                temp_mode->freq = 0;
            }
        }
        return max_mode;

}
           

繼續閱讀