zoukankan      html  css  js  c++  java
  • C之算法

       1° 选择排序算法

       核心思路如下图:

       以字符串排序进行说明

    #include <stdio.h>
    #include <string.h>
    #define SIZE 81
    #define LIM 20
    #define HALT " "
    void stsrt(char *strings[], int num);
    
    int main(void)
    {
        char input[LIM][SIZE];
        char *ptstr[LIM];
        int ct = 0;
        int k;
    
        printf("Input up %d lines, and I will sort them.
    ", LIM);
        printf("To stop, press the Enter key at a line's start.
    ");
        while(ct < LIM && gets(input[ct]) != NULL && input[ct][0] != '')
        {
            ptstr[ct] = input[ct];
            ct++;
        }
        stsrt(ptstr, ct);
        puts("
    Here's the sorted list: 
    ");
        for(k = 0; k < ct; k++)
            puts(ptstr[k]);
    
        return 0;
    }
    void stsrt(char *strings[], int num)
    {
        char *temp;
        int top, seek;
    
        for(top = 0; top < num - 1; top++)
            for(seek = top + 1; seek < num; seek++)
                if(strcmp(strings[top], strings[seek]) > 0)
                {
                    temp = strings[top];
                    strings[top] = strings[seek];
                    strings[seek] = temp;
                }
    }
    View Code

       以int数组排序进行说明(以C语言为例)

    #include <stdio.h>
    
    void sort(int a[], int len);
    int main(void)
    {
        int number[] = {2, 4, 7, 11, 789, 16, 89, 24, 27, 32, 36, 40, 46};
        int len = sizeof(number) / sizeof(number[0]);
        sort(number, len);
        for(int i = 0; i <len; i++)
            printf("%d ", number[i]);
        return 0;
    }
    void sort(int a[], int len)
    {
        int temp;
        for(int i = 0; i < len - 1; i++)
        {
            for(int j = i + 1; j < len; j++)
                if(a[i] > a[j]) // 小——>大      若a[i] < a[j]; 大——>小
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
        }
    }
    View Code

       (以JAVA语言为例)

    public class ArrayDemo {
    
        public static void main(String[] args) {
           int age[] = {31, 30, 18, 17, 8, 9, 1, 39};
           sort(age);
           print(age);
        }
        
        public static void print(int temp[]) {
            for(int i = 0; i < temp.length; i++)
                System.out.print(temp[i] + " ");
        }
        
        public static void sort(int temp[]) {
            for(int i = 0, t = 0; i < temp.length - 1; i++)
                for(int j = i + 1; j < temp.length; j++)
                    if(temp[j] < temp[i]) {
                        t = temp[j];
                        temp[j] = temp[i];
                        temp[i] = t;
                    }
        }
    }
    View Code

       核心思想:(查找和放置)选择剩余最大值的一个办法就是比较剩余数组的第一和第二个元素。如果第二个元素大,就交换这两个数据。想在比较第一个和第三个元素。如果第三个大,就交换这两个数据。每一次交换都把大的元素移到上面。继续这种方法,直到比较第一个和最后一个元素。完成以后,最大的数就在剩余数组的第一个元素中。此时第一个元素已经排好了序,但是数组中的其他元素还很混乱。

       2° 冒泡排序算法

       核心思路如下图:

       (以JAVA语言为例)   

    public class OtherDemo {
    
        public static void main(String[] args) {
            int[] arr = new int[] { 3, 1, 6, 5, 4 };
            //排序前
            print(arr);
            bubbleSort(arr);
            //排序后
            print(arr);
        }
        
        public static void print(int temp[]) {
            for (int i = 0; i < temp.length; i++) {
                if (i != temp.length - 1)
                    System.out.print(temp[i] + ", ");
                else
                    System.out.println(temp[i]);
            }
    
        }
        
        //冒泡排序
        public static void bubbleSort(int temp[]) {
            for (int i = 0; i < temp.length - 1; i++)
                for (int j = 0; j < temp.length - i - 1; j++) //-x:让每一次比较的元素减少,-1:避免下标越界
                    if (temp[j] > temp[j+1]) {
                        int t = temp[j];
                        temp[j] = temp[j+1];
                        temp[j+1] = t;
                    }
        }
    
    }
    View Code

       3° 二分搜索算法

       核心思路如下图:

      

       (以C语言为例)

    #include <stdio.h>
    
    int search(int key, int a[], int len);
    int main(void)
    {
        int k = 36;
        int number[] = {2, 4, 7, 11, 13, 16, 21, 24, 27, 32, 36, 40, 46};
        int r = search(k, number, sizeof(number) / sizeof(number[0]));
        if(r > -1)
            printf("%d
    ", number[r]);
        return 0;
    }
    // 二分搜索(排好序的数组)
    int search(int key, int a[], int len)
    {
        int ret = -1;
        int left = 0;
        int right = len - 1;
    
        while(right >= left)   // 条件应该是right >= left,而不应是right > left
        {
            int mid = (left + right) / 2;
            if(a[mid] == key)
            {
                ret = mid;
                break;
            }
            else if(a[mid] > key)
                right = mid - 1;
            else
                left = mid + 1;
        }
        return ret;
    }
    View Code

        (以JAVA语言为例)

       4° 一个统计单词数的小程序

    #include <stdio.h>
    #include <stdbool.h>
    #include <ctype.h>
    // 统计单词数的函数
    int count_word(char * str);
    
    int main(void)
    {
        char str[101];
    
        puts("请输入一个字符串:");
        gets(str);
        printf("统计的单词数是:%d", count_word(str));
    
        return 0;
    }
    int count_word(char * str)
    {
        bool inword = false;
        int n_words = 0;
    
        while(*str)
        {
            if(!isspace(*str) && !inword)
            {
                inword = true;
                n_words++;
            }
            if(isspace(*str) && inword)
                inword = false;
            str++;
        }
        return n_words;
    }
    View Code
  • 相关阅读:
    前端水印
    canvas中图片大小自适应
    了解CSS变量var
    vue报错Adjacent JSX elements must be wrapped in an enclosing tag.
    修改Batch3DModelContent的片元着色器:让白模穿品如的衣服
    Cesium 顶点着色器中求解模型坐标
    Centos6.5部署及yum源注意事项
    linux常用命令
    复旦大学数学学院 18 级本科生对高等代数学习方法指导书的评价
    复旦大学数学学院 17 级本科生对高等代数学习方法指导书的评价
  • 原文地址:https://www.cnblogs.com/yerenyuan/p/5068868.html
Copyright © 2011-2022 走看看