Algorithm In C Pdf

Posted By admin On 12.12.20
  1. Sorting Algorithms. A Sorting Algorithm is used to rearrange a given array or list elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of element in the respective data structure.
  2. Algorithm Analysis in C, second edition, published by Addison-Wesley. These answers reflect the state of the book in the first printing. Specifically omitted are likely programming assignments and any question whose solu-tion is pointed to by a reference at the end of the chapter. Solutions vary in degree of complete.
  1. Data Structures In C++ Pdf
  2. Algorithm In C Programming Language Examples Pdf
  3. Algorithms In C 3rd Pdf

Data Structures & Algorithms. The C Compiler. The source code written in the source file is the human readable source for your program. It needs to be 'compiled', to turn into machine language so that your CPU can actually execute the program as per the given instructions.

For all those who aspire to excel in competitive programming, only having a knowledge about containers of STL is of less use till one is not aware what all STL has to offer.
STL has an ocean of algorithms, for all < algorithm > library functions : Refer here.

Some of the most used algorithms on vectors and most useful one’s in Competitive Programming are mentioned as follows :

Non-Manipulating AlgorithmsDownload kruti dev fonts.


  1. sort(first_iterator, last_iterator) – To sort the given vector.
  2. reverse(first_iterator, last_iterator) – To reverse a vector.
  3. *max_element (first_iterator, last_iterator) – To find the maximum element of a vector.
  4. *min_element (first_iterator, last_iterator) – To find the minimum element of a vector.
  5. accumulate(first_iterator, last_iterator, initial value of sum) – Does the summation of vector elements
    // A C++ program to demonstrate working of sort(),
    #include <algorithm>
    #include <vector>
    usingnamespacestd;
    intmain()
    // Initializing vector with array values
    intn = sizeof(arr)/sizeof(arr[0]);
    for(inti=0; i<n; i++)
    sort(vect.begin(), vect.end());
    cout << 'nVector after sorting is: ';
    cout << vect[i] << ' ';
    // Reversing the Vector
    for(inti=0; i<6; i++)
    cout << *max_element(vect.begin(), vect.end());
    cout << 'nMinimum element of vector is: ';
    cout << 'nThe summation of vector elements is: ';
    }

    Output:

  1. count(first_iterator, last_iterator,x) – To count the occurrences of x in vector.
  2. find(first_iterator, last_iterator, x) – Points to last address of vector ((name_of_vector).end()) if element is not present in vector.
    // and find()
    #include <iostream>
    usingnamespacestd;
    intmain()
    // Initializing vector with array values
    intn = sizeof(arr)/sizeof(arr[0]);
    // last element
    // element not present
    cout << 'nElement found':
    }

    Output:

  1. binary_search(first_iterator, last_iterator, x) – Tests whether x exists in sorted vector or not.
  2. lower_bound(first_iterator, last_iterator, x) – returns an iterator pointing to the first element in the range [first,last) which has a value not less than ‘x’.
  3. upper_bound(first_iterator, last_iterator, x) – returns an iterator pointing to the first element in the range [first,last) which has a value greater than ‘x’.
    // C++ program to demonstrate working of lower_bound()
    #include <algorithm>
    #include <vector>
    {
    intarr[] = {5, 10, 15, 20, 20, 23, 42, 45};
    vector<int> vect(arr, arr+n);
    // Sort the array to make sure that lower_bound()
    sort(vect.begin(), vect.end());
    // Returns the first occurrence of 20
    autoq = lower_bound(vect.begin(), vect.end(), 20);
    // Returns the last occurrence of 20
    autop = upper_bound(vect.begin(), vect.end(), 20);
    cout << 'The lower bound is at position: ';
    cout << p-vect.begin() << endl;
    return0;

    Output:

Some Manipulating Algorithms

  1. arr.erase(position to be deleted) – This erases selected element in vector and shifts and resizes the vector elements accordingly.
  2. arr.erase(unique(arr.begin(),arr.end()),arr.end()) – This erases the duplicate occurrences in sorted vector in a single line.
    #include <algorithm>
    #include <vector>
    {
    intarr[] = {5, 10, 15, 20, 20, 23, 42, 45};
    vector<int> vect(arr, arr+n);
    cout << 'Vector is :';
    cout << vect[i]<<' ';
    // Delete second element of vector
    for(inti=0; i<5; i++)
    sort(vect.begin(), vect.end());
    cout << 'nVector before removing duplicate '
    for(inti=0; i<5; i++)
    vect.erase(unique(vect.begin(),vect.end()),vect.end());
    cout << 'nVector after deleting duplicates: ';
    cout << vect[i] << ' ';
    return0;

    Output:

  1. next_permutation(first_iterator, last_iterator) – This modified the vector to its next permutation.
  2. prev_permutation(first_iterator, last_iterator) – This modified the vector to its previous permutation.
    // C++ program to demonstrate working of next_permutation()
    #include <algorithm>
    #include <vector>
    {
    intarr[] = {5, 10, 15, 20, 20, 23, 42, 45};
    vector<int> vect(arr, arr+n);
    cout << 'Given Vector is:n';
    cout << vect[i] << ' ';
    // modifies vector to its next permutation order
    cout << 'nVector after performing next permutation:n';
    cout << vect[i] << ' ';
    prev_permutation(vect.begin(), vect.end());
    cout << 'nVector after performing prev permutation:n';
    cout << vect[i] << ' ';
    return0;

    Output:

  1. distance(first_iterator,desired_position) – It returns the distance of desired position from the first iterator.This function is very useful while finding the index.
    // C++ program to demonstrate working of distance()
    #include <iostream>
    usingnamespacestd;
    intmain()
    // Initializing vector with array values
    intn = sizeof(arr)/sizeof(arr[0]);
    cout << 'Distance between first to max element: ';
    max_element(vect.begin(), vect.end()));
    }

    Output:

Data Structures In C++ Pdf

More – STL Articles Free powerdesk for windows 10.

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


Algorithm In C Programming Language Examples Pdf

Algorithm In C Pdf

Recommended Posts:

Algorithms In C 3rd Pdf