Data Structures & Algorithms - Week 1 To 7
Data Structures & Algorithms - Week 1 To 7
Data Structures & Algorithms - Week 1 To 7
Algorithms
Week1
Software: C/C++ edittor
BC++, TC++
C-Free is a professional C/C++ integrated
development environment (IDE) that support multi-
compilers. Use of this software, user can edit, build,
run and debug programs freely.
Example:library
– is composed of elements
(books)
– Accessing a particular
book requires knowledge
of the arrangement of the
books
– Users access books only
through the librarian
the logical arrangement of data elements,
combined with
the set of operations we need to access the
elements.
Basic Data Structures
Structures include
– linked lists
– Stacks
– Queues
– binary trees
– …and others
What is Algorithm?
Algorithm:
– A computable set of steps to achieve a desired result
– Ralationship to Data Structure
Example: Find an element
2 4 6
1 3 5 7
1 2 3 4 5 6 7
Sumary
Chapter 1: C LANGUAGE
1. ADDRESS
2. POINTERS
3. ARRAYS
4. ADDRESS OF EACH ELEMENT IN AN ARRAY
5. ACCESSING & MANIPULATING AN ARRAY USING
POINTERS
6. ANOTHER CASE OF MANIPULATING AN ARRAY USING
POINTERS
7. TWO-DIMENSIONAL ARRAY
8. POINTER ARRAYS
9. STRUCTURES
10. STRUCTURE POINTERS
Chapter 1: C LANGUAGE
1. ADDRESS
For every variable there are two attributes:
address and value
2. POINTERS
1. is a variable whose value is also an address.
2. A pointer to an integer is a variable that can
store the address of that integer
ia: value of variable
&ia: address of ia
*ia means you are printing the value at the
location specified by ia
Chapter 1: C LANGUAGE
int i; //A
int * ia; //B
cout<<"The address of i "<< &i << " value="<<i <<endl;
cout<<"The address of ia " << &ia << " value = " << ia<< endl;
i = 10; //C
ia = &i; //D
Points to Remember
3. ARRAYS
1. An array is a data structure
2. used to process multiple elements with the same data
type when a number of such elements are known.
3. An array is a composite data structure; that means it
had to be constructed from basic data types such as
array integers.
1. int a[5];
2. for(int i = 0;i<5;i++)
1. {a[i]=i; }
Chapter 1: C LANGUAGE
a=b; //error
b=a; //OK
Chapter 1: C LANGUAGE
7. TWO-DIMENSIONAL ARRAY
int a[3][2];
Chapter 1: C LANGUAGE
8. POINTER ARRAYS
You can define a pointer array (similarly to an array of
integers).
In the pointer array, the array elements store the
pointer that points to integer values.
Chapter 1: C LANGUAGE
9. STRUCTURES
Structures are used when
you want to process data of
multiple data types
But you still want to refer to
the data as a single entity
Access data:
structurename.membernam
e
Chapter 1: C LANGUAGE
1. FUNCTION
2. THE CONCEPT OF STACK
3. THE SEQUENCE OF EXECUTION DURING A
FUNCTION CALL
4. PARAMETER PASSING
5. CALL BY REFERENCE
6. RESOLVING VARIABLE REFERENCES
7. RECURSION
8. STACK OVERHEADS IN RECURSION
9. WRITING A RECURSIVE FUNCTION
10. TYPES OF RECURSION
CHAPTER 2: FUNCTION & RECURSION
1. FUNCTION
– provide modularity to the software
– divide complex tasks into small manageable tasks
– avoid duplication of work
CHAPTER 2: FUNCTION & RECURSION
3. THE SEQUENCE OF
EXECUTION DURING A
FUNCTION CALL
Result:?
CHAPTER 2: FUNCTION & RECURSION
7. RECURSION
– A method of
programming
whereby a function
directly or indirectly
calls itself
– Problems: stop
recursion?
CHAPTER 2: FUNCTION & RECURSION
7. RECURSION
CHAPTER 2: FUNCTION & RECURSION
7. RECURSION:
Hanoi tower
CHAPTER 2: FUNCTION & RECURSION
7. RECURSION
CHAPTER 2: FUNCTION & RECURSION
7 2
1 3 2
1 1 2
1 0
Week3: Recursion Excercises (1)
Print triangle
c d
a b
Week3: Recursion Excercises (8)
7 2
1 3 2
1 1 2
1 0
Week3: Recursion Excercises (9)
Minesweeper
Week 4
How?
– Proceeds by sequentially comparing the key with
elements in the list
– Continues until either we find a match or the end
of the list is encountered.
– If we find a match, the search terminates
successfully by returning the index of the element
– If the end of the list is encountered without a
match, the search terminates unsuccessfully.
1. LINEAR (SEQUENTIAL) SEARCH
{find =i;
break;}
return find;
} average time: O(n)
2. BINARY SEARCH
int Search (int list[], int key, int left, int right)
{
if (left <= right) {
int middle = (left + right)/2;
if (key == list[middle])
return middle;
else if (key < list[middle])
return Search(list,key,left,middle-1);
else return Search(list,key,middle+1,right);
}
return -1;
}
3. COMPLEXITY OF ALGORITHMS
Linear Search
– O(n).
Binary Search
– O(log2 N)
Week 4
Introduction:
Bubble sorting is a simple sorting technique in
which we arrange the elements of the list by
forming pairs of adjacent elements. That
means we form the pair of the ith and (i+1)th
element. If the order is ascending, we
interchange the elements of the pair if the
first element of the pair is greater than the
second element.
1. BUBBLE SORT
Introduction:
Basic Idea: Insert a record R into a
sequence of ordered records: R1,R2,.., Ri
with keys K1 <= K2 <= ... <= Ki , such that,
the resulting sequence of size i+1 is also
ordered with respect to key values.
2. INSERTION SORT
2. INSERTION SORT
2. INSERTION SORT
Algorithm Insertion_Sort; (* Assume Ro has Ko = -maxint *)
void InsertionSort( Item &list[])
{ // Insertion_Sort
Item r;
int i,j;
list[0].key = -maxint;
for (j=2; j<=n; j++)
{ r=list[j];
i=j-1;
while ( r.key < list[i].key )
{// move greater entries to the right
list[i+1]:=list[i];
i:=i-1;
};
list[i+1] = r // insert into it's place
}
3. SELECTION SORT
struct node
{
int data;
struct node *link;
};
struct node *insert(struct node *p, int n) {
struct node *temp;
if(p==NULL){
p=new node;
if(p==NULL){
Data Structures printf("Error\n");
exit(0);
}
p-> data = n;
p-> link = p;
}
else{
temp = p;
while (temp-> link != p)
temp = temp-> link;
temp-> link = new node;
if(temp -> link == NULL){
printf("Error\n");
exit(0);
}
temp = temp-> link;
temp-> data = n;
temp-> link = p;
}
return (p);
Data Structures
INSERTING A NODE BY USING
RECURSIVE PROGRAMS
INSERTING A NODE BY USING
RECURSIVE PROGRAMS