CPlusOne
Notes, Syllabus, Questions, Ideas,
Books, Reference materials on Electronics


Quick Links
#Login #Sign up #Forum #Feedback

 

#Previous question #Index of C Questions #Next question

One element from the top is selected and is compares to the other rest of the elements down the line and inserted to another position and rest of the elements are shifted accordingly.

Code: Insertion Sort of 5 integer elements in ascending order.

void insertion_sort(int elements[], int count)
{
  int row, index;
  for(row = 1; row < count; row++)
  {
    int tmp = elements[row];
    for (index = row; index > 0 &&
      tmp < elements[index - 1]; index--) /* compare */
    {
      elements[index] = elements[index - 1]; /*move */
    }
    elements[index] = tmp; /* insert */
  }

}
int main(int argc, char *argv[])
{

  int array[5] = { 4, 9, 5, 1, 0 };

  insertion_sort(array, 5);

}

 Vote 0

Similar topics related to this section

structured programming language, first C program, header files, library, compilation, pre-processing, compilation, optimization, linking format, debugging with GDB, data types, storage classes, if, if-else, if-else-if, label and goto, switch case, loop statements, break and continue, pointers, enum type, macro, basic operators, logical operators, logical vs bitwise operators, bit shifting, bit set, reset/clear, toggle, Operator precedence, array, multidimensional arrays, user defined types, typedef, struct type, union type, struct vs union, struct pack padding, bit fields, structure as function argument, array as function argument, printing pointers, scanf string input, scan string with blanks, scanf string ends with newline, return of scanf, return of printf, scanf with printf, print using fprintf, take input using fscanf, floating point formatting, multiple arguments in printf and scanf, variadic functions, variadic macro, calling conventions, calling convention of C,C++ and PASCAL, operator precedence, ternary operator, switch statement, continue in switch and loops, for, while, do-while loop syntax, for to while, do-while and while usage, infinite loops, function declaration and definition, library function linking, call-by-value and call-by-reference, parameter passing call-by-value, parameter passing call-by-reference, address of local and dynamic variable, volatile variable, external vs static, global vs static variable, register vs auto variable, const, macro vs constant, debug builds, debug macros, compiler macro for C++, FILE, LINE, DATE, TIME, compiler marcos, token pasting, characterizing, const char *,char * const, const char * const, char array and char pointers, enum vs macro, write vs append mode, binary vs text mode, macro vs typedef, size of a structure, upper and lower 16bit of 32bit unsigned, near, far and huge pointer, typed pointers and void pointer, sizeof void, sizeof void and void pointer, operators for void pointer, l-value, struct and array, NULL pointer, inline-functions, macro vs inline-function, malloc vs calloc, function pointer, compare with ==, argc and argv, C startup routine, strcpy and strcat source, signed and unsigned compare, string vs integer pointer, xor operator, bitwise shifting, logical and bitwise or, macro definition, long pointer increment, default C++ argument value, short pointer increment, execution sequence, exceptions, setjump and longjump, trace a memory leak, detect memory corruption, C and Data Structures, Data Structures using array, Linked List, Single Linked list, Doubly Linked list, Circular Linked list, Doubly Circular Linked list, Tree and Binary tree, heap, Complexity, Linear Search, Binary Search, Hash Table, Sorting, Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Heap Sort, Merge sort, Radix Sort, Recursion, argv environ, system(),

# C/C++ Interview questions and answers - Shetty's World
# C Interview Questions and Answers - C Interview FAQs & C Books
# C interview questions and answers | TechInterviews
# C Interview Questions and Answers :: ALL Interview .com
# C Interview Questions and Answers

* #1 webmaster Sun 02 Feb/2014 03:44:48(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.