0% found this document useful (0 votes)
5 views6 pages

Data Structures and Algorithms Guide

The document provides an overview of data structures, including definitions, operations, and types such as linear and non-linear structures. It explains programming concepts like function prototypes, data types, and variable scopes, as well as loop types and control statements in C++. Additionally, it includes examples of sorting algorithms (Bubble sort, Insertion sort, Selection sort) and their efficiency comparisons.

Uploaded by

杨恺雄
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Data Structures and Algorithms Guide

The document provides an overview of data structures, including definitions, operations, and types such as linear and non-linear structures. It explains programming concepts like function prototypes, data types, and variable scopes, as well as loop types and control statements in C++. Additionally, it includes examples of sorting algorithms (Bubble sort, Insertion sort, Selection sort) and their efficiency comparisons.

Uploaded by

杨恺雄
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

TCYS133 Algorithm and Data Structure

Tutorial

1. Define what is meant by a data structure.

Data structure is the representation of the logical relationship existing between individual
elements of data. It indicates organizing the data in memory.

2. List any four operations that can be performed on data structures.

Traversing, Insertion, Deletion, Merging, Sorting, Searching

3. Differentiate between linear and non-linear data structures with one example
each.

-Linear data structure which is the arrangement of data is done in sequential manner. One
element is connected to only one other element in a linear form. Example: Arrays, Linked
Lists

-Non-Linear data structure which is the data values in this structure are not arranged in a
sequential order. Example: Trees, Graphs

4. What is the purpose of a function prototype in C++?

In C++, the function declaration code should normally appear before the function call.
However, if we want to define a function after the function call, we must use a function
prototype to declare it first.

5. Explain the data types in programming languages.

Data types specify the type of value a variable can hold.

Data Types Keyword Explanation

Boolean Bool Logical values(true/false)

Character Char Single characters

Integer Int Whole numbers

Floating point Float Single-precision decimal numbers


TCYS133 Algorithm and Data Structure

Double floating point Double Double-precision decimal numbers

Valueless Void Represents the absence of type

6. Name the types of relational and logical operators and their functions.

Relational Operators: Used to Compare two values

Operator Description

== Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to


TCYS133 Algorithm and Data Structure

Logical Operators: Used to combine conditional statements

Operator Description

&&(AND) True if both operands are non-zero

||(OR) True if any of the two operands is non-zero

!(NOT) Reverses the logical state of its operand

7. Explain the difference between a local and a global variable.

Global Variable: Declared in the main body of the source code, outside all functions.

Local Variable: Declared within the body of a function or a specific block.

8. Define the loop types and loop control statements. Apply answers in C++.

Loop Types:

Loop Types Description

While Loop Repeats a statement while a condition is true (tests condition before
execution).

For loop Executes a sequence of statements multiple times; manages loop


variables efficiently.

Do…. While Like a 'while' loop, but tests the condition at the end of the loop
body.

Nested loop One or more loop inside any another ‘while’, ‘for’, or ‘do…while’
TCYS133 Algorithm and Data Structure

loop.

Control Statements:

Types Description

Break Terminates the loop immediately.

Continue Skips the remainder of the loop body and retests the
condition.

Go to Transfers control to a labeled statement (not advised).

9. Give two examples of linear data structures.

Arrays, Linked lists, Stacks, or Queues

10. Give two examples of non-linear data structures.

Trees, Graphs, Tables, or Sets.

11. Consider the array of elements {64, 25, 12, 22, 11, 35, 18}, perform:

(i) Bubble sort.


Pass 1: {25, 12, 22, 11, 35, 18, 64} (64 moves to end)

Pass 2: {12, 22, 11, 25, 18, 35, 64} (35 moves to sorted position)

Pass 3: {12, 11, 22, 18, 25, 35, 64} (25 moves to sorted position)

Pass 4: {11, 12, 18, 22, 25, 35, 64} (22 moves to sorted position)

Sorted: {11, 12, 18, 22, 25, 35, 64}


TCYS133 Algorithm and Data Structure

(ii) Insertion sort.


Initial: {| 64, 25, 12, 22, 11, 35, 18} (

Pass 1 (Insert 25): {25, 64, 12, 22, 11, 35, 18}

Pass 2 (Insert 12): {12, 25, 64, 22, 11, 35, 18}

Pass 3 (Insert 22): {12, 22, 25, 64, 11, 35, 18}

Pass 4 (Insert 11): {11, 12, 22, 25, 64, 35, 18}

Pass 5 (Insert 35): {11, 12, 22, 25, 35, 64, 18}

Pass 6 (Insert 18): {11, 12, 18, 22, 25, 35, 64}

(iii) Selection sort.


Pass 1: Min is 11. Swap with 64. {11, 25, 12, 22, 64, 35, 18}

Pass 2: Min (in remaining) is 12. Swap with 25. {11, 12, 25, 22, 64, 35, 18}

Pass 3: Min is 18. Swap with 25. {11, 12, 18, 22, 64, 35, 25}

Pass 4: Min is 25. Swap with 64. {11, 12, 18, 22, 25, 35, 64}

Pass 5: Min is 35. No swap needed. {11, 12, 18, 22, 25, 35, 64}

(iv) Analyze the efficiency of the sorting algorithm in (i), (ii), (iii).

Insertion sort > Selection sort > Bubble sort

12. Consider the array of elements {50, 23, 9, 18, 61, 32, 43}, perform:

(i) Bubble sort.


Pass 1: {23, 9, 18, 50, 32, 43, 61} (61 bubbles to end)
TCYS133 Algorithm and Data Structure

Pass 2: {9, 18, 23, 32, 43, 50, 61} (50 bubbles to sorted pos)

(ii) Insertion sort.


Initial: {| 50, 23, 9, 18, 61, 32, 43}

Pass 1 (Insert 23): {23, 50, 9, 18, 61, 32, 43}

Pass 2 (Insert 9): {9, 23, 50, 18, 61, 32, 43}

Pass 3 (Insert 18): {9, 18, 23, 50, 61, 32, 43}

Pass 4 (Insert 61): {9, 18, 23, 50, 61, 32, 43} (No change)

Pass 5 (Insert 32): {9, 18, 23, 32, 50, 61, 43}

Pass 6 (Insert 43): {9, 18, 23, 32, 43, 50, 61}

(iii) Selection sort.


Pass 1: Min is 9. Swap with 50. {9, 23, 50, 18, 61, 32, 43}

Pass 2: Min is 18. Swap with 23. {9, 18, 50, 23, 61, 32, 43}

Pass 3: Min is 23. Swap with 50. {9, 18, 23, 50, 61, 32, 43}

Pass 4: Min is 32. Swap with 50. {9, 18, 23, 32, 61, 50, 43}

Pass 5: Min is 43. Swap with 61. {9, 18, 23, 32, 43, 50, 61}

Pass 6: Min is 50. Swap with 61. {9, 18, 23, 32, 43, 50, 61}

(v) Analyze the efficiency of the sorting algorithm in (i), (ii), (iii).
Insertion sort > Selection sort > Bubble sort

You might also like