Basic C language algorithms and C language Algorithms

Source: Internet
Author: User

Basic C language algorithms and C language Algorithms

//
// main.c
// select sort
//
// Created by king on 15/10/20.
// Copyright © 2015 king. All rights reserved.
//

#include <stdio.h>

int main (int argc, const char * argv []) {
    // define the array
    int array [5] = {23, 56, 36, 89, 50};
    
    // Calculate the length of the array
    int length = sizeof (array) / sizeof (array [0]);
    
    // Traverse the array (unordered)
    for (int i = 0; i <length; i ++) {
        printf ("array [% d] =% d \ n", i, array [i]);
    }
    
    printf ("---------------------- \ n");
    
    // length-1 prevents crossing, because the last one does not need to be compared
    for (int i = 0; i <length-1; i ++) {
        
        for (int j = i + 1; j <length; j ++) {
            
            if (array [i]> array [j]) {
                
                // Define temporary variables to exchange variables
                int temp = array [i];
                array [i] = array [j];
                array [j] = temp;
                
            }
        }
    }
    
    // Traverse the array (after sorting)
    for (int i = 0; i <length; i ++) {
        printf ("array [% d] =% d \ n", i, array [i]);
    }
    return 0;
}
//
// main.c
//  Bubble Sort
//
// Created by king on 15/10/20.
// Copyright © 2015 king. All rights reserved.
//

#include <stdio.h>

int main (int argc, const char * argv []) {
    // define the array
    int array [5] = {23, 56, 36, 89, 50};
    
    // Calculate the length of the array
    int length = sizeof (array) / sizeof (array [0]);
    
    // Traverse the array (unordered)
    for (int i = 0; i <length; i ++) {
        printf ("array [% d] =% d \ n", i, array [i]);
    }
    
    printf ("---------------------- \ n");
    
    for (int i = 0; i <length-1; i ++) {
        
        for (int j = 0; j <length-1 -i; j ++) {
            // Pairwise comparison
            if (array [j]> array [j + 1]) {
                // Swap variables
                int temp = array [j];
                array [j] = array [j + 1];
                array [j + 1] = temp;
            }
        }
    }
    
    // Traverse the array (after sorting)
    for (int i = 0; i <length; i ++) {
        printf ("array [% d] =% d \ n", i, array [i]);
    }
    
    return 0;
}
//
// main.c
// Find in half
//
// Created by king on 15/10/20.
// Copyright © 2015 king. All rights reserved.
//

#include <stdio.h>
int findKey (int array [], int length, int key);

int main (int argc, const char * argv []) {
  
    // define the array
    int nums [10] = {15, 20, 35, 40, 46, 56, 59, 68, 76, 90};
    // array length
    int length = sizeof (nums) / sizeof (nums [0]);
    // The key to be found
    int key = 46;
    
    printf ("% d == nums [% d] \ n", key, findKey (nums, length, key));
    return 0;
}

#pragma mark --Find key
int findKey (int array [], int length, int key)
{
    int min, max, mid;
    min = 0;
    max = length-1;
    
    // as long as it is still within our range
    while (min <= max) {
        // calculate intermediate value
        mid = (min + max) / 2;
        if (key> array [mid]) {
            min = mid + 1;
        } else if (key <array [mid])
        {
            max = mid-1;
        } else
        {
            return mid;
        }
        
    }
    return -1; // return -1 if not found
}
Table lookup

//
// main.c
// base lookup table method
//
// Created by king on 15/10/20.
// Copyright © 2015 king. All rights reserved.
//

// Decimal to hexadecimal octal binary

#include <stdio.h>
void total (int value, int base, int offset);
void ptintBinary (int num);
void printfOct (int num);
void printfHex (int num);

int main (int argc, const char * argv []) {
    
    printf ("Hexadecimal:"), printfHex (101010);
    printf ("Octal:"), printfOct (10545);
    printf ("Binary:"), ptintBinary (9546);
    
    return 0;
}

#pragma mark-convert to hex
void printfHex (int num)
{
    total (num, 15, 4);
}

#pragma mark-turn octal
void printfOct (int num)
{
    total (num, 7, 3);
}
#pragma mark-Convert to binary
void ptintBinary (int num)
{
    total (num, 1, 1);
}

/ **
 * Convert all bases
 *
 * @param value The value to be converted
 * @param base requires the number on &
 * @param offset The number of digits to be shifted to the right
 * /
void total (int value, int base, int offset)
{
    // 1. Define an array to store all values in hexadecimal
    char charValues [] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    // 2. Define an array to save the results after the query
    char results [32] = {'0'};
    // 3. Define a variable to record the index that needs to be stored in the query result array
    int pos = sizeof (results) / sizeof (results [0]);
    
    while (value! = 0) {
        // 1. Take out the 1-bit value
        int res = value & base; // 1 7 15
        // 2. Use the retrieved value to query the corresponding result in the table
        char c = charValues [res];
        // 3. Store the result of the query
        results [-pos] = c;
        // 4. Remove the 1 bit that the binary has been taken
        value = value >> offset; // 1 3 4
    }
    
    // 4. Print the result
    for (int i = pos; i <32; i ++) {
        printf ("% c", results [i]);
    }
    printf ("\ n");
    
}

Related Article

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.