Coder Social home page Coder Social logo

arrays's Introduction

Searching and Sorting in Arrays

Searching in Arrays

  1. Linear Search
// Time Complexity:- O(N)
// Auxiliary Space:- O(1)

int linearSearch( int arr [], int n, int x ) {
    for ( int i = 0; i < n; i++ ) {
        if ( arr[i] == x ) return i;
    }
    return -1;
}
  1. Binary Search
// Time Complexity:- O(logN)
// Auxiliary Space:- O(1)

int binarySearch(int arr [], int n, int x) {
    int l = 0, r = n - 1;
    while ( l <= r ) {
        int m = (l + r) / 2;
        if ( arr[m] == x )
            return m;
        else if ( arr[m] < x )
            l = m + 1;
        else
            r = m - 1;
    }
    return -1;
}

Sorting in Arrays

  1. Bubble Sort
// Time Complexity:- O(N^2)
// Auxiliary Space:- O(1) 

void swap( int * x, int * y ) {
    int temp = *x;
    *y = *x;
    *x = temp;
}

void bubbleSort( int arr [], int n ) {

    for ( int i = 0; i < n-1; i++ ) {

        int swapped = false;

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

        if ( swapped == false ) break;

    }

}
  1. Insertion Sort
// Time Complexity:- O(N^2) 
// Auxiliary Space:- O(1)

void swap( int * x, int * y ) {
    int temp = *x;
    *y = *x;
    *x = temp;
}

void insertionSort( int arr [], int n ) {

    for ( int i = 0; i < n - 1; i++ ) {

        if ( arr[i] > arr[i+1] ) {

            swap(arr[i], arr[i+1]);

            for ( int j = i ; j >= 1; j-- ) {
                if ( arr[j] < arr[j-1] ) swap(arr[j], arr[j-1]);   
            }

        }

    }

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.