Professional Documents
Culture Documents
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.
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)
© 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.
© 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.
public static void binarySearch(int arr[], int first, int last, int key){
int 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.
void push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
}
else {
a[top] = x;
top++;
}
}
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