天天看點

C語言如何寫出高效代碼?8個高效代碼案例賞析,絕對幹貨

作者:曉亮Albert

當涉及複雜的高效C代碼案例時,這些代碼示例展示了C語言中一些複雜且高效的應用案例,涵蓋了排序算法、圖算法、位操作、檔案操作、多線程程式設計等領域。這些案例展現了C語言在各個領域的廣泛應用和高效性,以下是8個經典的例子:

1.快速排序算法:

void quicksort(int arr[], int low, int high) {
    if (low < high) {
        int pivot = partition(arr, low, high);
        quicksort(arr, low, pivot - 1);
        quicksort(arr, pivot + 1, high);
    }
}

int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j <= high - 1; j++) {
        if (arr[j] <= pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}
void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
           

這是經典的快速排序算法實作,采用分治思想,通過遞歸地将數組分成兩部分并進行排序,進而實作快速的排序效果。

2.動态規劃算法-最長公共子序列(Longest Common Subsequence):

int lcs(char* X, char* Y, int m, int n) {
    int L[m + 1][n + 1];
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0 || j == 0)
                L[i][j] = 0;
            else if (X[i - 1] == Y[j - 1])
                L[i][j] = L[i - 1][j - 1] + 1;
            else
                L[i][j] = max(L[i - 1][j], L[i][j - 1]);
        }
    }
    return L[m][n];
}

int max(int a, int b) {
    return (a > b) ? a : b;
}
           

這段代碼實作了最長公共子序列問題的動态規劃解法,通過建構一個二維數組,逐漸計算最長公共子序列的長度。

3.哈夫曼編碼(Huffman Coding):

struct MinHeapNode {
    char data;
    unsigned freq;
    struct MinHeapNode *left, *right;
};

struct MinHeap {
    unsigned size;
    unsigned capacity;
    struct MinHeapNode** array;
};

struct MinHeapNode* newNode(char data, unsigned freq) {
    struct MinHeapNode* temp =
        (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
    temp->left = temp->right = NULL;
    temp->data = data;
    temp->freq = freq;
    return temp;
}

struct MinHeap* createMinHeap(unsigned capacity) {
    struct MinHeap* minHeap =
        (struct MinHeap*)malloc(sizeof(struct MinHeap));
    minHeap->size = 0;
    minHeap->capacity = capacity;
    minHeap->array =
        (struct MinHeapNode**)malloc(minHeap->capacity * 
                                     sizeof(struct MinHeapNode*));
    return minHeap;
}

void buildMinHeap(struct MinHeap* minHeap, int size) 
{ 
  int i; 
  for (i = (size - 1) / 2; i >= 0; --i) 
    minHeapify(minHeap, i); 
}

void minHeapify(struct MinHeap* minHeap, int idx) 
{ 
  int smallest = idx; 
  int left = 2 * idx + 1; 
  int right = 2 * idx + 2;
  if (left < minHeap->size && 
      minHeap->array[left]->freq < minHeap->array[smallest]->freq)
    smallest = left;

if (right < minHeap->size && 
    minHeap->array[right]->freq < minHeap->array[smallest]->freq)
    smallest = right;

if (smallest != idx) {
    swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
    minHeapify(minHeap, smallest);
}
}

void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{ struct MinHeapNode* t = *a; *a = *b; *b = t; }

int isSizeOne(struct MinHeap* minHeap) { return (minHeap->size == 1); }

struct MinHeapNode* extractMin(struct MinHeap* minHeap) { 
  struct MinHeapNode* temp = minHeap->array[0]; 
  minHeap->array[0] = minHeap->array[minHeap->size - 1]; 
  --minHeap->size; 
  minHeapify(minHeap, 0); 
  return temp; }

void insertMinHeap(struct MinHeap* minHeap, struct MinHeapNode* minHeapNode) 
{ 
  ++minHeap->size; 
  int i = minHeap->size - 1; 
  while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) 
  { 
    minHeap->array[i] = minHeap->array[(i - 1) / 2]; 
    i = (i - 1) / 2; 
  } 
  minHeap->array[i] = minHeapNode; 
}

void buildHuffmanTree(char data[], int freq[], int size) 
{ struct MinHeapNode* left, * right, * top; 
 struct MinHeap* minHeap = createMinHeap(size); 
 for (int i = 0; i < size; ++i) 
   minHeap->array[i] = newNode(data[i], freq[i]); 
 minHeap->size = size; 
 buildMinHeap(minHeap, size); 
 while (!isSizeOne(minHeap)) 
 { 
   left = extractMin(minHeap); 
   right = extractMin(minHeap); 
   top = newNode('#39;, left->freq + right->freq); 
   top->left = left; 
   top->right = right; 
   insertMinHeap(minHeap, top); 
 } 
 printCodes(minHeap->array[0], ""); 
}

void printCodes(struct MinHeapNode* root, char* str) 
{ 
  if (root == NULL) return; 
  if (root->data != '#39;) 
    printf("%c: %s\n", root->data, str); 
  printCodes(root->left, strcat(str, "0")); 
  str[strlen(str) - 1] = '\0'; 
  printCodes(root->right, strcat(str, "1")); 
  str[strlen(str) - 1] = '\0';
}           

這段代碼實作了哈夫曼編碼的算法,通過建構哈夫曼樹并生成每個字元的編碼。

4.圖的深度優先搜尋(Depth-First Search):

#define MAX_VERTICES 100

typedef struct {
    int data[MAX_VERTICES];
    int top;
} Stack;

void initStack(Stack* stack) {
    stack->top = -1;
}

void push(Stack* stack, int value) {
    stack->data[++stack->top] = value;
}

int pop(Stack* stack) {
    return stack->data[stack->top--];
}

int isEmpty(Stack* stack) {
    return stack->top == -1;
}

void dfs(int graph[MAX_VERTICES][MAX_VERTICES], int vertex, int visited[]) {
    Stack stack;
    initStack(&stack);
    push(&stack, vertex);
    visited[vertex] = 1;
    while (!isEmpty(&stack)) {
        int current = pop(&stack);
        printf("%d ", current);
        for (int i = 0; i < MAX_VERTICES; ++i) {
            if (graph[current][i] && !visited[i]) {
                push(&stack, i);
                visited[i] = 1;
            }
        }
    }
}
           

這段代碼實作了圖的深度優先搜尋算法,使用棧資料結構來輔助周遊圖的頂點,并通過通路數組來記錄已通路的頂點。

5.最短路徑算法-迪傑斯特拉算法(Dijkstra's Algorithm):

#define MAX_VERTICES 100
#define INF 9999

void dijkstra(int graph[MAX_VERTICES][MAX_VERTICES], int startVertex, int numVertices) {
    int dist[MAX_VERTICES];
    int visited[MAX_VERTICES];
    for (int i = 0; i < numVertices; ++i) {
        dist[i] = INF;
        visited[i] = 0;
    }
    dist[startVertex] = 0;
    for (int count = 0; count < numVertices - 1; ++count) {
        int u = minDistance(dist, visited, numVertices);
        visited[u] = 1;
        for (int v = 0; v < numVertices; ++v) {
            if (!visited[v] && graph[u][v] && dist[u] + graph[u][v] < dist[v]) {
                dist[v] = dist[u] + graph[u][v];
            }
        }
    }
    printDistances(dist, numVertices);
}

int minDistance(int dist[], int visited[], int numVertices) {
    int min = INF, minIndex;
    for (int v = 0; v < numVertices; ++v) {
        if (!visited[v] && dist[v] <= min) {
            min = dist[v];
            minIndex = v;
        }
    }
    return minIndex;
}

void printDistances(int dist[], int numVertices) {
    printf("Vertex\tDistance from Start Vertex\n");
    for (int i = 0; i < numVertices; ++i) {
        printf("%d\t%d\n", i, dist[i]);
    }
}
           

這段代碼實作了迪傑斯特拉算法,用于求解帶權重的圖中從起始頂點到其他頂點的最短路徑。

6.位操作-計算整數中1的個數:

int countSetBits(int n) {
    int count = 0;
    while (n)
    {
     if (n == 0) 
      return 0;
     else 
      return 1 + countSetBits(n & (n - 1));
    }
}           

這段代碼使用位操作技巧計算一個整數中包含的1的個數,通過不斷将整數n與(n-1)進行按位與操作,直到n變為0,統計操作次數即為1的個數。

7.檔案操作-統計檔案中單詞的個數:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LENGTH 100

int countWords(FILE* file) {
    int count = 0;
    char word[MAX_LENGTH];
    while (fscanf(file, "%s", word) != EOF) {
        count++;
    }
    return count;
}
int main() {
    FILE* file = fopen("input.txt", "r");
    if (file == NULL) {
        printf("File not found.\n");
        return 1;
    }
    int wordCount = countWords(file);
    printf("Number of words in the file: %d\n", wordCount);
    fclose(file);
    return 0;
}
           

這段代碼讀取檔案中的内容,按照空格分隔單詞,并統計檔案中單詞的個數。

8.多線程程式設計-并行計算:

#include <stdio.h>
#include <pthread.h>

#define NUM_THREADS 4
#define ARRAY_SIZE 10000

int array[ARRAY_SIZE];
int sum = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* calculateSum(void* arg) {
    int threadID = *((int*)arg);
    int start = threadID * (ARRAY_SIZE / NUM_THREADS);
    int end = start + (ARRAY_SIZE / NUM_THREADS);
    int localSum = 0;
    for (int i = start; i < end; ++i) {
        localSum += array[i];
    }
    pthread_mutex_lock(&mutex);
    sum += localSum;
    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

int main() {
    pthread_t threads[NUM_THREADS];
    int threadIDs[NUM_THREADS];

    for (int i = 0; i < ARRAY_SIZE; ++i) {
        array[i] = i + 1;
    }

    for (int i = 0; i < NUM_THREADS; ++i) {
        threadIDs[i] = i;
        pthread_create(&threads[i], NULL, calculateSum, (void*)&threadIDs[i]);
    }

    for (int i = 0; i < NUM_THREADS; ++i) {
        pthread_join(threads[i], NULL);
    }

    printf("Sum of array elements: %d\n", sum);

    return 0;
}
           

這段代碼使用多線程程式設計實作并行計算一個數組中元素的總和,通過将數組劃分為多個部分,并配置設定給不同的線程進行計算,最後将各線程的計算結果累加得到最終的總和。