Posts

Showing posts from January, 2019

Quick Sort Algorithm

Image
Quick Sort: Quick Sort algorithm is based on divide-and-conquer approach, recursively processing small and small parts of array list at each level. Here is the three-step divide-and-conquer process for sorting a typical subarray A[p....r]. Divide:  Partition(rearrange) the array A[p ...r] into two sub-arrays A[p ...q-1] and A[q+1 ...r] such that each element of A[p...q-1] is less than and or equal to A[q], which is, in turn less than and or equal to A[q+1 ..r]. Compute the index q as part of this partitioning procedure. Conquer: Sort the two subarray A[p ...q-1] and A[q+1 ..r] by recursive calls of quicksort. Combine: Subarrays are sorted in place, no work is needed to combine them.   Quick Sort algorithm chooses an element as a pivot. And partitions the given array around choose the pivot element. There are many ways to choose a pivot element for partition. Always choose the first element as a pivot. Always choose the last element as a pivot. Choose a random element a

Developing First Program in C

Prerequisite 1. Editor 2. Compiler 3. Run-time environment (OS). Editor + Compiler are availble as IDE (Integrated Development Environment). Such as Turbo C/C++, Dev  C++, codeblocks.  Ex: #include <stdio.h> #include <conio.h> void main() {     clrscr();     printf("Welcome to C\n");     getch(); } #include: #include is a directive that calls the preprocessor to include the header file with our program.  Header file :                       Header file contains the declaration of the predefined function.   <stdio.h>   --> standard input output header file. This header file contain standard input/output related predefine function. Ex: printf(), scanf(), putc(), puts(), gets(), getc().... etc. <conio.h> -->   console input output header file. This header file contains console screen related input/output  related predefine. Ex: clrscr(), clreol(), insline(), gotoxy(), getch(), getche(), cprintf(), cscanf().... etc.  main(

Introduction of C

                          Basics of C Q-1 What is C?. Ans- C is a computer Programming language developed by Dennis Ritchie in 1972 at AT & T laboratory the USA. Q-2 What is Computer? Ans- A computer is an electronic machine that takes some data as input through the input device and after processing the data produced some information as output on the output device. Programming Language:- A language in that we can write the program is called a programming language. Q-2 What is Program? A program is a set of instruction. Q-2 What is Instruction? Instruction is a command given to the computer to perform some task. Q-2 What is need of programming language? The programming language is used to develop the software. The software is used to provide communication between human and machine. In other word, languages are the medium of communication between human and machine. NOTE:- Whatever code we write in the program which is the form of a high-level language ( Human

Pointers in C and C++

Image
Pointers in C and C++ A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.  Pointers store address of variables or a memory location.   Syntax: datatype *var_name; Ex: int *ptr;    To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x. #include <stdio.h>   int main() {     int x;       // Prints address of x     printf("%p", &x);       return 0; } EX: #include <stdio.h> int main() {    int num = 10;    printf("Value of variable num is: %d", num);    /* To print the address of a variable we use %p     * format specifier and ampersand (&) sign just     *

Manipulating Strings in C++

Introduction A string is a sequence of character. We have used null terminated <char> arrays (C-strings or C-style strings) to store and manipulate strings. ANSI C++ provides a class called string. Commonly Used String Constructors String();    // For creating an empty string. String(const char *str);    // For creating a string object from a null-terminated string. String(const string &str);    // For creating a string object from other string object Creating String Objects string s1, s3;    // Using constructor with no arguments. string s2(“xyz”);    // Using one-argument constructor. s1 = s2;        // Assigning string objects s3 = “abc” + s2;    // Concatenating strings cin >> s1;        // Reading from keyboard (one word) cout << s2;        // Display the content of s2 getline(cin, s1)    // Reading from keyboard a line of text s3 += s1;        // s3 = s3 + s1; s3 += “abc”;        // s3 = s3 + “abc”; Manipulating String Objects string s1(“12345”); string s