Data Structure and Algorithms Introduction:
A data structure is an arrangement of data in a computer's memory or even disk storage. An example of several common data structures are arrays, linked lists, queues, stacks, binary trees, and hash tables. Algorithms, on the other hand, are used to manipulate the data contained in these data structures as in searching and sorting.
C provides us with just one built in data structure, the array. Although efficient and easily used and understood, arrays often don't provide us with the level of functionality we need from a data structure. If the quantity of data we need to store is not well known at compile time, then using arrays could waste memory if too large, or waste time in resizing at runtime if too small. Several, more abstract, data structures can be constructed to better serve our needs.
Many algorithms apply directly to a specific data structures. When working with certain data structures you need to know how to insert new data, search for a specified item, and deleting a specific item.
Commonly used algorithms include are useful for:
• Searching for a particular data item (or record).
• Sorting the data. There are many ways to sort data. Simple sorting, Advanced sorting
• Iterating through all the items in a data structure. (Visiting each item in turn so as to display it or perform some other action on these items)
Recursive Functions:
Functions are used to encapsulate a set of operations and return information to the main program or calling routine. The use of functions provides several benefits. First, it makes programs significantly easier to understand and maintain. The main program can consist of a series of function calls rather than countless lines of code. A second benefit is that well written functions may be reused in multiple programs. The C standard library is an example of the reuse of functions. A third benefit of using functions is that different programmers working on one large project can divide the workload by writing different functions. A recursive function is a function that calls itself, thus understanding recursion to understand recursion seems like a call to itself.
a) Algorithm to find Fibonacci Series:
i Get the input number n, i.e. the number of terms to be generated in the series.
ii Print the first two numbers of the series as 0 and 1, assign these two values to
variables a and b.
iii Compute the next element in the series as the sum of the previous two elements.
iv Keep on sliding the variables a and b to b and c to compute the series.
b) Algorithm to find the factorial of a given number using recursion:
i. Get the number N
ii. call the function for N
iii. Inside the function compute fact = N * fact (N-1)
iv. Step ((3) is repeated till N becomes 1.
v. Return the fact value to the main function and display the result.
c) Algorithm to sort a set of numbers using arrays:
i Initialize an array of required size.
ii Get the input set of numbers and store them in the array.
iii Take each number of an array and compare that number with the subsequent
numbers in the array.
iv If the former is greater than the latter, swap the two numbers.
v Repeat the procedure for all the numbers in the array.
vi At the end of the procedure, the array would be sorted and is printed.
Post A Comment:
0 comments: