Search and Sort

    • Header file
    • Binary search
    • Selection Search
    • Bubble sort
    • Heap sort
    • Insertion sort
    • Summary

I study Data Structure and Algorithm in GeeksforGeeks and play with the algorithms in C. In this part, I will cover algorithms of searching and sorting!

Header file

Since that some methods are used quite often, I write a header file for this part.

/* File name: util.h */
#include <stdio.h>

/* Function to swap two values */
void swap(int *a, int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
}

/* Function to print an array */
void printArray(int arr[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

This algorithm supposes that an array is already sorted and search the sub-array in dependent of the middle number and our search number. Its time complexity T(n)=O(log[n]) .

#include <stdio.h>
// Binary search for half part recursively

int binary_search(int arr[], int l, int r, int x){
    if (r >= l){
        int middle = l + (r-1)/2;
        if ( arr[middle] == x) return middle;
        if ( arr[middle] < x) return binary_search(arr, middle+1, r, x);
        return binary_search(arr, l, middle-1, x);
    }
    return -1;
}

int main(void){

    int arr[] = {1, 2, 4, 5, 6, 9};
    int n = sizeof(arr)/ sizeof(arr[0]);
    int x = 9;
    int result = binary_search(arr, 0, n-1, x);

    (result == -1)? printf("Not Found!\n")
                  : printf("Element is presented at index %d\n", result );

    return 0;
}

This algorithm finds the smallest element repeatedly in the unsorted part and stacks it at the beginning. Its time complexity is T(n)=O(n2) . I use recurrence in this case, but you can use two nested loops, either. It is quite the same.

#include <stdio.h>

void selectionsort(int arr[], int l, int r){
    if (r >= l){
        int flag = l;
        int i;
        // Find the smallest index 'flag'
        for (i = l + 1; i <= r; i++){
            if (arr[flag] > arr[i]) flag = i;
        }
        // Swap arr[flag] and arr[l]
        int temp = arr[flag];
        arr[flag] = arr[l];
        arr[l] = temp;
        selectionsort(arr, l+1, r);
    }
}

int main(void){
    int j ;
    int arr[] = {1, 5, 3, 2, 9, 7, 6};
    int n = sizeof(arr)/ sizeof(arr[0]);
    selectionsort(arr, 0, n-1);
    for (j = 0; j < n; j++)
        printf("%d\n", arr[j] );
}

Note: Selection sort makes O(n) swaps which is minimum among all sorting algorithms mentioned above.

Bubble sort

Bubble sort is the simplest sorting algorithm. It works by continuously comparing the element with the unsorted sub-part. In the first iteration, it results in a largest or smallest element to be placed in the end or the beginning of an array respectively. Such method is continue on with the unsorted array. Time complexity: T(n)=O(n2) .

#include <stdio.h>
#include "util.h"

void bubbleSort(int arr[], int n){
    int i, j;

    for (i = 0; i < n -1; i++){
        for (j = 0; j < n - i -1; j++){
            if (arr[j] > arr[j+1]) swap(&arr[j], &arr[j+1]);
        }
    }
}

// Driver program to test above functions
int main()
{
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("Sorted array: \n");
    printArray(arr, n);
    return 0;
}

Heap sort

Insertion sort

Summary

Worst case complexities:
Merge Sort — nLogn
Bubble Sort — n2
Quick Sort — n2
Selection Sort — n2

Best case complexities:
Bubble Sort – n

你可能感兴趣的:(算法,结构)