Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Linear search, Bubble sort, System.Array class

Notes:

I. Linear search : [Starts at: 00min:00sec]
II. Bubble sort :[Starts at: 09min:48sec]
III. System.Array class: [Starts at: 23min:15sec]

I. Linear search : [Starts at: 00min:00sec]
Searching an element in a given array

Example Code:

using UnityEngine;
public class ArraysDemo : MonoBehaviour {
void Start () {

// Linear Searching

int[] numbers = new int[5]{3,4,5,8,1};
int item = 5;
bool found = false;

for (int i = 0; i < numbers.Length; i++) {
if (numbers [i] == item) {
found = true;
break;
}
}
if (found == true)
Debug.Log ("Item Found");
else
Debug.Log ("Item not found");
}
}

II. Sorting (Bubble sort) :[Starts at: 09min:48sec]
Sorting n numbers in ascending and descending order

Example Code:

using UnityEngine;
public class ArraysDemo : MonoBehaviour {
void Start () {

// Sorting : Bubble sort

int[] numbers = new int[3]{3,1,2};

for (int i = 0; i < numbers.Length -1; i++) {
for (int j = i+1; j < numbers.Length; j++) {
if (numbers [i] > numbers [j]) {
int temp = numbers [i];
numbers [i] = numbers [j];
numbers [j] = temp;
}
}
}

for (int i = 0; i < numbers.Length; i++) {
Debug.Log (numbers [i]);
}

}
}

III. System.Array class: [Starts at: 23min:15sec]

Example Code:

using UnityEngine;
using System;
public class ArraysDemo : MonoBehaviour {
void Start () {

// Array class methods and instance methods

int[] numbers = new int[3]{3,1,2};

numbers.SetValue (20, 1); // numbers[1]=20;

Debug.Log (numbers.GetValue (1)); // numbers[1]

}
}