You are on page 1of 5

Sorting:

1-Selection Sort:
• Find the minimum element on array.
• Swap it with first element.
• Start from the next element.
• And So on until the array is sorted.

Time complexity is O(n^2)

public static void selectionSort(int[] arr){


for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
// Searching for Lowest index
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;
}
}
// Swap it with first element in you array
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}

2- Bubble sort:
• Compare element with next element.
• If next element is smaller swap them.
• Else skip them.
• After every loop the biggest number be last element on the array.
• Start again until your array being sorted.
Time complexity is O(n^2)

static void bubbleSort(int[] arr) {


int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//if element bigger than next swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
} } } }

© Mahmoud Abdelazim
3- Insertion sort:
• Use first element as a key.
• Compare this key to next element.
• Insert next element in his correct order in left subarray.
• And use the last element in left subarray as a new Key.
• And so on until your array is sorted.

Time complexity is O(n^2)

public static void insertionSort(int array[]) {


int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}

© Mahmoud Abdelazim
Searching:
You need to search about number in array so there is two ways to do that.
1- Linear Search: Check every number in the array if that is we search about.

Time complexity is O(n)

public static int linearSearch(int[] arr, int key){


for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}

2- Binary Search: search algorithm works on Sorted Arrays only


It searches in the array half by half:

• Get the middle of the array.


• Look is it the number you want.
• If yes return it.
• Else Look is middle bigger or smaller than the number you want.
• If bigger do the last steps in the right part of the array.
• If smaller do the last steps in the left part of the array.
• Repeat the last steps until getting your result.
Time complexity is O(log(n))

public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;

while( first <= last ){

if ( arr[mid] < key ){


first = mid + 1;
}
else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}
else{
last = mid - 1;
}

mid = (first + last)/2;


}

© Mahmoud Abdelazim
Stack:
Stack Is a simple data structures works as next picture.
Time Complexities of operations on stack:
push(), pop(), isEmpty() and top() all
take O(1) time. We do not run any loop in
any of these operations.

1- Push: function to insert elements to our stack.

void push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
}

else {
a[top] = x;
top++;
}
}

2- Pop: function to delete the last element on stack.


int pop(){
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}

else {
int x = a[top];
top--;
return x;
}
}
3- Top: function to show the last element on stack without deleting it
int top()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}

© Mahmoud Abdelazim
4- Traverse or Print: function to print all elements on stack one by one.
void print(){
for(int i = top;i>-1;i--){
System.out.print(" "+ a[i]);
}
}

© Mahmoud Abdelazim

You might also like