Sunday, April 4, 2010

insertion sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
Simple implementation
Efficient for (quite) small data sets
Adaptive, i.e. efficient for data sets that are already substantially sorted: the time complexity is O(n + d), where d is the number of inversions
More efficient in practice than most other simple quadratic (i.e. O(n2)) algorithms such as selection sort or bubble sort: the average running time is n2/4,[citation needed] and the running time is linear in the best case
Stable, i.e. does not change the relative order of elements with equal keys
In-place, i.e. only requires a constant amount O(1) of additional memory space
Online, i.e. can sort a list as it receives it.
SELECTION SORT
Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations.

Greeting.h: interface for the Greeting class.
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_GREETING_H__34246B3E_5382_401E_9265_7B6DFEFA39AF__INCLUDED_)
#define AFX_GREETING_H__34246B3E_5382_401E_9265_7B6DFEFA39AF__INCLUDED_
#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000
class Greeting {public:void PrintMessage();Greeting();virtual ~Greeting();
private:char* message;};
#endif // !defined(AFX_GREETING_H__34246B3E_5382_401E_9265_7B6DFEFA39AF__INCLUDED_)
Select Greeting.cpp file and edit it. Enter a line
message="Hello World!\n";
inside the Greeting class constructor
Greeting::Greeting(){message = "Hello World\n";}
Enter line of code inside the PrintMessage function
printf(message);
void Greeting::PrintMessage(){ printf(message);}
Enter a line #include <> under existing #include lines
#include "stdafx.h"#include "Greeting.h"#include
Your Greeting.cpp file should be like this:
Greeting.cpp: implementation of the Greeting class.
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"#include "Greeting.h"#include
//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////
Greeting::Greeting(){ message = "Hello World\n";}
Greeting::~Greeting(){}
void Greeting::PrintMessage(){ printf(message);}
Select HelloWorld.cpp file and enter the following two line of code in main function.
Greeting gr;gr.PrintMessage();
Now your HelloWorld.cpp file must be like this:
// HelloWorld.cpp : Defines the entry point for the console application.
#include "stdafx.h"#include "Greeting.h"
int main(int argc, char* argv[]){Greeting gr;gr.PrintMessage();return 0;}

bubble sort

Bubble sort
Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort.
// Example Program in C++ // to sort an array
// using bubble sort

#include

void main(void)
{
int temp, i, j;
int ar[10];

cout<<"enter elements of array:\n";
for(i=0; i<10; i++)
cin>>ar[i];

// sorting is done here
for(i=0;i<10;i++)
for(j=0; j<(10-1); j++)
if (ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
// till here

for(i=0; i<10; i++)
cout<
cout< }

CS-10

sorting
In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions:
The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order);
The output is a permutation, or reordering, of the input.
Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956.[1] Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2004). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and lower bounds.