Simple operation and sorting of an array 4th discussion topic (for WJ)

Source: Internet
Author: User
Tags random seed

(Class-assigned assignments) defines an integer array num[n] as an external, with six functions:

    1. void main (void);
    2. bubble function void bubble_sort (void);
    3. Select Sort a function void select_sort1 (void);
    4. Select sort two function void select_sort2 (void);
    5. Random number initialization array function void init (void);//random seed, each number less than .
    6. Print output function void prt (void);//12 hits per line

Handle Num[n] Array descending !

7. Write another function??? del (int); removes a number from the first to the end of the num[n] integer array (entered from the keyboard in main), if the deletion succeeds, the remaining number remains in descending order, and the first number is 55. .

8. Initializing an external array num[n], writing another function

The void Odd_even (void) Implementation arranges the odd array elements in the front and even after the arrays. (Do not allow other array-assisted operations to be defined)

The construction of the function:

1. Initializing an array of num

void Init ()// initialize num, randomly generating 0-55 of data {        int  i;            Srand ((unsigned) time (NULL));          for (i=0; i<maxn;i++)    {        Num[i]=rand ()%; // random numbers are generated in 0-55.             }    }
randomly generated array num

2. Bubble sort

voidBublle_sort ()//bubble Sort (descending){        inti,j,temp;  for(i=0; i<maxn-1; i++)    {                 for(j=0; j<maxn-i-1; j + +)        {                        if(num[j]<num[j+1]) {temp=Num[j]; NUM[J]=num[j+1]; Num[j+1]=temp; }                    }            }    }
Bublle_sort ()

3. Select sort 1:

voidSelect_sort1 ()//Select Sort (Descending){        inti,j,k,temp;  for(i=0; i<maxn-1; i++) {k=i;  for(j=i+1; j<maxn;j++)        {                        if(Num[j]>num[k]) k=j;//Select the number of large I, with Num[i] Exchange position                    }                if(k!=i) {temp=Num[k]; NUM[K]=Num[i]; Num[i]=temp; }            }    }
Select_sort1 ()

4. Select Sort 2

voidSelect_sort2 ()//Select sort 2 (Descending){        inti,j,k,temp;  for(i=0; i<maxn-1; i++) {k=i;  for(j=i+1; j<maxn;j++)        {                        if(num[j]>Num[k]) {Temp=Num[k]; NUM[K]=Num[j]; NUM[J]=temp; }                    }            }    }
Select_sort2 ()

5. Output sequencing

voidPRT ()//12 per line, processing Num[n] array descending! {    inti;    Select_sort2 ();  for(i=0; i<maxn;i++)    {                if(i% A== One|| i==maxn-1) printf ("%2d\n", Num[i]); Elseprintf"%2d", Num[i]); }}
prt ()

6. Set the number of array I to 55

void del (int  x) {    int  i;      for (i=0; i<maxn;i++)    {        if(x==num[i])        {            num[i]=;             return  ;        }    }    printf (" Delete failed! \ n");}
del (int x)

7, odd in front, even in the rear

voidOdd_even ()//The implementation arranges the odd array elements in the front and even in the rear. {    inti,j,temp;  for(i=0; i<maxn-1; i++)//Similar selection sort    {        if(num[i]%2)Continue;  for(j=i+1; j<maxn;j++)        {                        if(num[j]%2) {Temp=Num[i]; Num[i]=Num[j]; NUM[J]=temp; Continue; }            }         Break; }}
Odd_even ()

8. Complete code:

#include <stdio.h>#include<stdlib.h>//header file for random functions (rand ())#include <time.h>//the header file (srand (unsigned) time (NULL)) of the temporal function assists in generating random numbers#defineMAXN 100intNUM[MAXN];voidInit ()//Initialize num to randomly generate 0-55 of the data{        inti;        Srand ((unsigned) time (NULL));  for(i=0; i<maxn;i++) {Num[i]=rand ()% About;//random numbers are generated in 0-55.            }    }voidBublle_sort ()//bubble Sort (descending){        inti,j,temp;  for(i=0; i<maxn-1; i++)    {                 for(j=0; j<maxn-i-1; j + +)        {                        if(num[j]<num[j+1]) {temp=Num[j]; NUM[J]=num[j+1]; Num[j+1]=temp; }                    }            }    }voidSelect_sort1 ()//Select sort 2 (Descending){        inti,j,k,temp;  for(i=0; i<maxn-1; i++) {k=i;  for(j=i+1; j<maxn;j++)        {                        if(Num[j]>num[k]) k=j;//Select the number of large I, with Num[i] Exchange position                    }                if(k!=i) {temp=Num[k]; NUM[K]=Num[i]; Num[i]=temp; }            }    }voidSelect_sort2 ()//Select sort 2 (Descending){        inti,j,k,temp;  for(i=0; i<maxn-1; i++) {k=i;  for(j=i+1; j<maxn;j++)        {                        if(num[j]>Num[k]) {Temp=Num[k]; NUM[K]=Num[j]; NUM[J]=temp; }                    }            }    }voidprint () {inti;  for(i=0; i<maxn;i++)    {                if(i% A== One|| i==maxn-1) printf ("%2d\n", Num[i]); Elseprintf"%2d", Num[i]); }}voidPRT ()//12 per line, processing Num[n] array descending! {select_sort2 (); Print ();}voidDelintx) {    inti;  for(i=0; i<maxn;i++)    {        if(x==Num[i]) {Num[i]= -; return ; }} printf ("Delete failed! \ n");}voidOdd_even ()//The implementation arranges the odd array elements in the front and even in the rear. {    inti,j,temp;  for(i=0; i<maxn-1; i++)//Similar selection sort    {        if(num[i]%2)Continue;  for(j=i+1; j<maxn;j++)        {                        if(num[j]%2) {Temp=Num[i]; Num[i]=Num[j]; NUM[J]=temp; Continue; }            }         Break; }}voidMain () {inti;    Init (); printf ("initialize the num array: \ n");    Print (); printf ("the effect of the \nprt () function \ n");    PRT ();    Init ();    Select_sort1 (); printf ("\ n Select sort 1:\n");        Print ();    Init ();    Select_sort2 (); printf ("\ n Select sort 2:\n");    Print ();    Init ();    Bublle_sort (); printf ("\ n Bubble sort: \ n");    Print (); Del ( to); printf ("\ nthe first 31 is removed and set to 55:\n");    Print ();    Init ();    Odd_even (); printf ("\ nthe odd number is placed on the head, even at the tail: \ n");    Print (); }

Implementation results:

Simple operation and sorting of an array 4th discussion topic (for WJ)

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.