0% found this document useful (0 votes)
2 views10 pages

Arrays

The document provides an overview of arrays in programming, focusing on single and multidimensional arrays, including their definitions, types, and operations such as searching and sorting algorithms. It covers concepts like linear and binary search, various sorting algorithms, and the structure of jagged and rectangular arrays. Additionally, it discusses the Array class in C#, its methods, and advanced topics related to collections.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Arrays

The document provides an overview of arrays in programming, focusing on single and multidimensional arrays, including their definitions, types, and operations such as searching and sorting algorithms. It covers concepts like linear and binary search, various sorting algorithms, and the structure of jagged and rectangular arrays. Additionally, it discusses the Array class in C#, its methods, and advanced topics related to collections.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

16/09/2024

Objectives
▸Single Dimension arrays
▸Multidimensional Arrays
- Two-Dimensional Arrays
- Jagged Array
Fundamentals of Programming
▸The Array class
▸Advanced topics

Arrays

By: Võ Văn Hải


Email: [email protected]

1 [email protected] 2

1 2

Arrays
Introduction
▸An array is a group of like-typed variables that are referred to by a common
name.
- Each data item is called an element of the array.
- The data types of the elements may be any valid data type like char, int, float, etc.
- The elements are stored in a contiguous location.
- Length of the array specifies the number of elements present in the array.
- The allocation of memory for the arrays is done dynamically.
- Arrays are kinds of objects;
• Passing by reference
- The variables in the array are ordered and each has an index beginning from 0.
Single Dimension Arrays
▸Types of arrays:
- Single Dimension
- Multiple Dimension

[email protected] 3 4

3 4

1
16/09/2024

Single Dimension Arrays Single Dimension Arrays


Introduction Declaration and Initialization
▸ C# array is an object of base type System.Array. ▸Declare Syntax < Data Type > [ ] < Name_Array >
▸ Default values of numeric array and reference type elements are set to be - Example
respectively zero and null. int[] x; // can store int values
string[] s; // can store string values
▸ Array elements can be of any type, including an array type.
double[] d; // can store double values
▸ Array types are reference types which are derived from the abstract base type Array. ▸Initialization
These types implement IEnumerable and for it, they use foreach iteration on all arrays
in C#. type [ ] < Name_Array > = new < datatype > [size];

▸Array Declaration and Initialization

▸Array Initialization after Declaration

[email protected] 5 [email protected] 6

5 6

Single Dimension Arrays Single Dimension Arrays


Access Array Elements Change Array Elements
▸We can access the elements in the array using the index of the array. ▸To change the element, we simply assign a new value to that particular
index

Access Item

Access Item
using iterator

[email protected] 7 [email protected] 8

7 8

2
16/09/2024

Algorithms on Single Dimension Arrays


Searching Algorithms – Linear Search
▸The idea behind linear search is to search a given element in an array.
Hence, we have to consider below steps:
1. A given array of size n (let's take size as 5).
2. An item to be searched in given array.
3. Go through the array till the last element in the array.
4. If a match is found, return the index position and if not then will return -1.

Algorithms on Single Dimension Arrays

9 [email protected] 10

9 10

Algorithms on Single Dimension Arrays Algorithms on Single Dimension Arrays


Searching Algorithms - Binary Search Searching Algorithms - Binary Search
▸The Binary Search Algorithm is one of the Divide and conquer algorithm
types, where it halves the number of elements it has to search in each
step. It works on a sorted array.

Steps
1. Each step compares the search key with the value of the middle searching the value of 37 in array
element of the array.
2. The keys matching in step 1 means a matching element has
been found, and its index (or position) is returned. Else step 3
or 4.
3. If the search key is less than the middle element, then the
algorithm repeats its action on the sub-array to the left of the
middle element or,
4. If the search key is greater than the middle element, then the
algorithm repeats its action on the sub-array to the right of the
middle element.
5. If the search key does not match any of the subsequent left or
right arrays, it means that the key is not present in the array,
and a special "Nil" indication can be returned.
Animation: https://yongdanielliang.github.io/animation/web/BinarySearchNew.html
[email protected] 11 [email protected] 12

11 12

3
16/09/2024

Algorithms on Single Dimension Arrays Algorithms on Single Dimension Arrays


Comparison of 2 Algorithms - Sort
classic searching
algorithms ▸A Sorting Algorithm is used to rearrange a given array or list of elements
according to a comparison operator on the elements. The comparison operator is
used to decide the new order of elements in the respective data structure.
▸ Selection Sort ▸ Cycle Sort
▸ Bubble Sort ▸ Cocktail Sort
▸ Insertion Sort ▸ Strand Sort
▸ Merge Sort ▸ Bitonic Sort
▸ Quick Sort ▸ Pancake sorting
▸ Heap Sort ▸ BogoSort or Permutation Sort
▸ Counting Sort ▸ Gnome Sort
▸ Radix Sort ▸ Sleep Sort – The King of Laziness
▸ Bucket Sort ▸ Structure Sorting in C++
▸ Bingo Sort Algorithm ▸ Stooge Sort
▸ ShellSort ▸ Tag Sort (To get both sorted and original)
▸ TimSort ▸ Tree Sort
▸ Comb Sort ▸ Odd-Even Sort / Brick Sort
▸ Pigeonhole Sort ▸ 3-way Merge Sort
[email protected] 13 [email protected] 14

13 14

Algorithms on Single Dimension Arrays Algorithms on Single Dimension Arrays


Algorithms - Bubble Sort Algorithm Algorithms – Selection Sort Algorithm

Algorithm
1. Find the smallest element in an array, replace it
with the first element.
2. Find the smallest element in the unsorted part of
an array, replace it with the second element.
3. Repeat this process until the end and array will
be sorted at the end.

[email protected] 15 [email protected] 16

15 16

4
16/09/2024

Algorithms on Single Dimension Arrays Single Dimension Arrays


Algorithms - Insertion Sort Algorithm Exercises
Insertion sort is basically for a small number of elements. In this algorithm,
1. compare every element with the previous element. ▸Create a random integer values array, then create functions that:
2. If the previous element is greater than current element, move that (previous) element to the 1. to calculate the average value of array elements.
next position. 2. to test if an array contains a specific value.
3. If not, continue to find a great number.
3. to find the index of an array element.
4. to remove a specific element from an array.
5. to find the maximum and minimum value of an array.
6. to reverse an array of integer values.
7. to find duplicate values in an array of values.
8. to remove duplicate elements from an array.

[email protected] 17 [email protected] 18

17 18

Algorithms on Single Dimension Arrays


Exercises
▸Create a C# program that
- requests 10 integers from the user and orders them by implementing the bubble sort
algorithm.
- Request a sentence from the user, then ask to enter a word. Search if the word
appears in the phrase using the linear search algorithm.

Multidimensional Arrays

[email protected] 19 20

19 20

5
16/09/2024

Multidimensional Arrays Multidimensional Arrays


Introduction Rectangular Arrays
▸The arrays which store the elements in the form of rows and columns are ▸C# Multi-dimensional arrays are supported up to 32 dimensions.
called Two-Dimensional Array in C#. The two-dimensional array which is ▸We can declare the C# multidimensional arrays by adding commas in the
also called a multidimensional array is of two types in C#. They are as square brackets. For instance, [,] declares a two-dimensional array, [ , , ]
follows declares a 3-D array, [, , ,] declares a 4-D array, and so on.
- Rectangular Array: The array whose rows and columns are equal is called a
- No. Of commas = Dimensions – 1
rectangular array
- Jagged Array: The array whose rows and columns are not equal is called a jagged ▸Example
array

[email protected] 21 [email protected] 22

21 22

Multidimensional Arrays Multidimensional Arrays


Two-Dimensional Arrays Two-Dimensional Arrays - creating
▸A two-dimensional (2-D) array is the most basic form of C# ▸Syntax
multidimensional array. It can be imagined as a list of arrays. data_type [,] array_name = new data_type[no_rows,no_cols]
▸Data in multidimensional arrays are stored in tabular form (in row major ▸Example
order).

▸Initialization

▸The elements have been represented by a[i][j], where i is the row


number, and j is the column number.
- Or just simple:

[email protected] 23 [email protected] 24

23 24

6
16/09/2024

Multidimensional Arrays Multidimensional Arrays


Two-Dimensional Arrays – Accessing elements Two-Dimensional Arrays - exercises
Accessing elements Change elements

▸Looping through a 2-D Array Loop through all elements in 2-D arrays

[email protected] 25 [email protected] 26

25 26

Multidimensional Arrays Multidimensional Arrays


Jagged Arrays -Introduction Jagged Arrays - creating
▸C# jagged array is an array of arrays, which means it’s an array that ▸Syntax
contains other arrays (inner arrays for clarity). data_type[][] arrayName = new data_type [rows][ ];
▸When you create a jagged array, you declare the number of fixed rows in ▸Example
the array. Then, each row can have a different number of columns.
- To be more specific, jagged is a single-dimensional array, where each row will hold
another array of different dimensions
▸Initialization

[email protected] 27 [email protected] 28

27 28

7
16/09/2024

Multidimensional Arrays Multidimensional Arrays


Jagged Arrays – accessing elements Jagged Arrays - Iterating through
▸Syntax arrayName[rowIndex][columnIndex]

▸Example

▸ jaggedArray.Length - gives the size of jagged array


fruits[1][1]; ▸ jaggedArray[i].Length - gives the size of elements
of the ith array inside the jagged array

Loop Through Jagged


Array Elements Using
fruits[1]; foreach Loop

[email protected] 29 [email protected] 30

29 30

Multidimensional Arrays Multidimensional Arrays


Jagged Array vs Multidimensional Array Jagged Array – exercises 1
1. Create a jagged array and initialize it using the following values for its rows and columns;
Then, display it.
11111
22
3333
44
2. Create a Jagged Array with random integer numbers (or by user input) by getting the
number of rows and columns from the user and printing the data in the array to the user.
Then, create functions to implement following tasks:
1. Print the biggest number of each row and the largest number of the whole array.
2. Sort values ascending of each row.
3. Print items of the array that are prime.
4. Search and print all positions of a number (enter from the user).

[email protected] 31 [email protected] 32

31 32

8
16/09/2024

Multidimensional Arrays The Array class


Jagged Array - exercises ▸The System.Array class
- Parent of all arrays
▸The X company has 3 working groups; group 1 has 5 members, group 2 - All arrays inherit from it
has 3 members, and group 3 has 6 members. The data stored for each - All arrays have the same:
member has an ID number, full name, and completed tasks. An ID - Basic functionality
identifies each member. - Basic properties
- E.g. Length property
▸Select an appropriate data structure to save this info. Then, write
functions to perform the following tasks: ▸Methods
- Rank – number of dimensions
1. Initialize an array with pre-assigned values ​or values ​entered from the keyboard.
- Length – number of all elements through all dimensions
2. Print a list of all members.
- GetLength(index) – returns the number of elements in the specified dimension
3. Print the information on a member when the ID is known. - GetEnumerator() – returns IEnumerator for the array elements
4. Print the member with the highest number of completed tasks. - BinarySearch(…) – searches for a given element into a sorted array (uses binary
1. Create the main program with menus that allow you to select the tasks search)
- IndexOf(…) – searches for a given element and returns the index of the first
to be performed. occurrence (if any)
Hint: create a jagged array to store these members in the form [id,[name, tasks]]. - LastIndexOf(…) – searches for a given element and returns the last occurrence index
- Copy(src, dest, len) – copies array elements; has many overloads

[email protected] 33 [email protected] 34

33 34

The Array class Advanced topics


▸Methods C# Collections
- Reverse(…) – inverts the arrays elements upside down
- Clear(…) – assigns value 0 (null) for each elements
- CreateInstance(…) – creates an array
- Accepts as parameters the number of dimensions, start index and number of
elements
- Implements ICloneable, IList, ICollection and IEnumerable interfaces
- Sort(Array) – sorts array elements ▸ArrayList ▸List<T>
- Elements should implement IComparable
▸HashTable ▸Dictionary<TKey,TValue>
- Sort(Array, IComparer) – sorts array elements by given external IComparer
▸SortedList ▸SortedList<TKey,TValue>
- Sort(Array, Comparison<T>) – sorts array elements by given comparison operation
▸Stack ▸Stack<T>
▸Queue ▸Queue<T>
▸HashSet<T>
▸LinkedList<T>
▸… ▸…

https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-8.0
[email protected] 35 [email protected] 36

35 36

9
16/09/2024

[email protected]

37

10

You might also like