Introduction to Data Structures and Algorithm

Overview

The stud of data structure algorithm is critical to the development of the professional programmer. Data structures is important since it dictates the types of operations we can perform on the data and how efficiently they can be carried out. It also dictates how dynamic w can be in dealing with our data; hence, data structuring simplifies the manipulation of data. In the design of many types of programs, the choice of data structures is primary design consideration, as experience in building large systems has shown that the difficulty of implementation and the quality of the final result depends heavily on choosing the best data structure. After the data structures are chosen, and then the algorithms to be used often become relatively obvious. Sometimes things work in the opposite direction - data structures, but in either case the choice of appropriate data structures is crucial.

a data structure is defined as a collection of related data and a set of rules for organizing and accessing it. In computer science, a data structure is a particular means of data organization and storage in a computer so that these data can b used efficiency. Data structures are used in almost every program or software system. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. Specific data structures are essential ingredients of many efficient algorithms, and make possible the management of huge amount of data, such as large databases and internet indexing services. Some formal design methods and programming languages emphasize data structures, rather than algorithms, as the key organizing factor in software design.

Friday 23 September 2011

Basics


Looping
A loop is a programming language construction that allows the programmer to instruct the computer to perform a certain instruction, or set of instructions over and over again.

There are many different types, but some are omitted from programming languages by virtue of the fact that they are variations on an existing loop type, and as such the same behavior can be achieved without offering a specific loop type to cover it.

However, we cover the three types which are common to most programming languages:

Condition Tested Loops
Counted Loops
Endless Loops
Of these, only the condition tested loop is vital; the other two just give a more convenient way of achieving the same effect.


For Loop
Program that uses for "int i" loops [C#]

using System;

class Program
{
    static void Main()
    { 
//
// Shows five 'for int i' loops.
//
Console.WriteLine("--- For 1 ---"); for (int i = 0; i < 10; i++) { Console.WriteLine(i); } Console.WriteLine("--- For 2 ---"); for (int i = 10 - 1; i >= 0; i--) { Console.WriteLine(i); } Console.WriteLine("--- For 3 ---"); for (int i = 0; i < 10; i += 2) { Console.WriteLine(i); } Console.WriteLine("--- For 4 ---"); for (int i = 10 - 1; i >= 0; i -= 2) { Console.WriteLine(i); } Console.WriteLine("--- For 5 ---"); for (int i = 0; i < (20 / 2); i += 2) { Console.WriteLine(i); } } } Output --- For 1 --- 0 1 2 3 4 5 6 7 8 9 --- For 2 --- 9 8 7 6 5 4 3 2 1 0 --- For 3 --- 0 2 4 6 8 --- For 4 --- 9 7 5 3 1 --- For 5 --- 0 2 4 6 8
While Loop
Program that uses while loop with condition [C#]
using System;

class Program
{
    static void Main()
    {
 
// Continue in while loop until index is equal to ten.
int i = 0; while (i < 10) { Console.Write("While statement ");
// Write the index to the screen.
Console.WriteLine(i);
// Increment the variable.
i++; } } } Output While statement 0 While statement 1 While statement 2 While statement 3 While statement 4 While statement 5 While statement 6 While statement 7 While statement 8 While statement 9
Do While Loop
Program that uses do while loop [C#]

class Program
{
    static void Main()
    {
 int[] ids = new int[] { 6, 7, 8, 10 };
 
//
// Use do while loop to sum numbers in 4-element array.
//
int sum = 0; int i = 0; do { sum += ids[i]; i++; } while (i < 4); System.Console.WriteLine(sum); } } Output 31
Foreach Loop
Program that uses foreach over array [C#]

using System;

class Program
{
    static void Main()
    {
 
// Use a string array to loop over.
string[] ferns = { "Psilotopsida", "Equisetopsida", "Marattiopsida", "Polypodiopsida" };
// Loop with the foreach keyword.
foreach (string value in ferns) { Console.WriteLine(value); } } } Output Psilotopsida Equisetopsida Marattiopsida Polypodiopsida

If Statement
The if statement in the C# language is a selection statement. It is implemented with intermediate language instructions
called branches. With if, we can make a logical decision based on a parameter or input from a user. Here, we look at if.

This program simply computes the value of an expression and then tests it in an if-statement. The condition inside the if-statement is evaluated
to a boolean value and if the value is true, the inner block is executed.
Program that uses if-statement [C#]

using System;

class Program
{
    static void Main()
    {
 
// Call method with embedded if-statement three times.
int result1 = Test(0); int result2 = Test(50); int result3 = Test(-1);
// Print results.
Console.WriteLine(result1); Console.WriteLine(result2); Console.WriteLine(result3); } static int Test(int value) { if (value == 0) { return -1; } else if (value <= 10) { return 0; } else if (value <= 100) { return 1; } else
// See note on "else after return"
{ return 2; } } } Output -1 1 0