Wednesday, December 12, 2018

Sorting and Searching



Sorting Defination:
it needs to speed up searching operation in a list.

There is 2 Type of Sorting:
-          Ascending
-          Descending

And there is 2 difficulties of sorting:
Simple:
                        Bubble sort
      Selection sort
      Insertion sort
Intermediate:
 Quick Sort
                   Merge Sort

Sorry But in this blog we only learn simple sorting because i also still learning about it

Syntax for Bubble Sort:
void Bubble(int *DataArr, int n)
{
                        int i, j;
                        for(i=1; i<n; i++)
                                    for(j=n-1; j>=i; j--)
                                                if(DataArr[j-1] > DataArr[j])
                         Swap(&DataArr[j-1],&DataArr[j]);
}

Syntax for  Selection Sort:
for(i=0; i<N-1; i++){      /* N=number of data */
            Set idx_smallest equal to i
            for(j=i+1; j<N; j++){
                        If array[ j ] < array [ idx_smallest ] then idx_smallest = j
    }
            Swap array[ i ] with array[ idx_smallest ]
}

Syntax for Insertion Sort:
for(i=1; i<n; i++) {
               x = A[i], insert x to its suitable place between A[0] and A[i-1].
}

So thats all for now untill the next update for the intermidiate one hehehe..

Searching Defination:
Searching is an action to retrieve information based on particular key from some saved information

Several types of searching algorithm:
1.      Linear Search
Linear search compares each element of the array with the search key.

2.      Binary Search:
                        The linear searching method works well for small or unsorted arrays. However, for large arrays linear searching is inefficient

3.      Interpolation Search
            technique is performed on the sorted data, This searching process is almost similar with binary search technique, and This searching technique is done with the approximate location of the data


Syntax for Linear Search:
1. n : total record of array x.
2. For each x[i], 0 £  i £ n-1, check whether x[i] = key.
3. If x[i] = key, then the searched data is found in index=i. Finished.
4. If x[i] ¹ key, then continue searching until the last data which is i = n-1.
5. If i= n-1 and x[i] ¹ key, it means the data is not exist in the list, and set index = -1. Finished.

Syntax for Binary Search:
1. n : total record of array x.
2. left=0,  right= n-1.
3. mid =(int) (left + right)/2.
4. If x[mid]=key then index = mid. Finished.
5. If x[mid]<key then left = mid+1.
6. If x[mid]>key then right = mid-1.
7. If left £ right and x[mid] ¹ key, then repeat point 3.
8. If x[mid] ¹ key then index = -1. Finished.

Syntax for Interpolation Search:
1.      In the interpolation search, we'll split the data according to the following formula:

2.      If data[mid] = sought data, data has been found, searching is stopped and return mid.
3.      If data[mid]!= sought data, repeat point **
4.      **Searching is continued while sought data > data[min] and sought data < data[max].
5.      Looking for a mid value by entering into the interpolation formula
6.      If data[mid] > sought data, high = mid – 1
7.      If data[mid] < sought data, low = mid + 1
8.      It will be looped until the requirements point ** are not met then return (-1), data not found



Wednesday, December 5, 2018

File Processing

Files Defination:
-         File is a collection of record
-         Record is a collection of field
-         Field is a block of byte
-         Byte is collection of bit

Stream Defination:
To keep key in data from keyboard need to be saved at secondary storage device as a data file.

So without any further do lets take a look to the main part of this blog
Creating File:
So in order to use a algorithm for file i will give a screenshot of my work




If you want to create a file you also need something like lets say a database.but i will create that databse in .txt to make it ez, but you cant make the database in any diffrent type of file as long it supporting to create a text but i suggest you to create the file in .txt


As you can see at my first screenshot. In that syntax there is ‘w’,’r’
So i will explain that below:
r                               opening a file to be read.
            w                              creating a file to be written.
            a                               opening a File for data append.
            r+                             opening a File for read/write.
            w+                           creating file for read/write.
            a+                            opening a File for read/append
            “rb”                            opening a File (binary) to be read.
            “wb”               creating a file (binary) for write operation.
These are command to do with the .txt file 

 Albert TG/2201736385/LB04
#Binusian2022