SORTING AND SEARCHING FILE
Heyya ladss, back with me, the student of computer science in Binus University, so after we've been learning about file processing, we will move on to searching and sorting file. So, basically there are 5 types of sorting in a file, so what do we need sorting for bruh? analogically, do you prefer to search for your comic books in your bookselves just by searching it when it's mixed up with various comics and random volume numbers, or when it's sorted out comics by comics, and the volumes number are in sequence? Rethorical, isn't it? especially when you're dealing with a large amount of datas, that's why sorting is quite crucial, and the types of sorting are various too, starting with easy concept of sorting till complicated one, so if you have a small amount of data, you can use easy one, but if not, you'd rather use complicated ones, so classyfing by the complexity of sorting method, we can rank it from the easiest which is : bubble sort, selection sort, insertion sort, and the ones that are considered intermediate are quick sort and merge sort. Depending to the number of datas and the sequences, bubble sort may takes a few days, or even weeks, that can be sorted by merge sort and will only take only a few minutes. Meanwhile for searching, it's also rethorical of how important it is, just imagine that you have a large amount of database in excel for shipping data, you can obviously imagines how hard is it to search for the particular shipping data that you want one by one from the top line until you find it, not only that, the data you have may also have the same data in most column, the item name that is being shipped may be the same, or the client's name, or even the destination,etc. ,and it's a pain to look at every lines and column one by one.There are 3 main types which are linear search,binary search, and interpolation search. The basic differences are : linear uses a search key, which means the data can be found literally anywhere, you can find it in the beginning or even in the very end. It's not much different from searching manually one by one, but the point is that it searchs based on a certain key, that differentiates one data from another, instead of looking at it one by one. Meanwhile, for binary search, it's usually used when you have much more data than you can handle by linear such, the main point is splitting your data by 2 sides, and goes on to the side where your data exists, and repeat you process of splitting data until your data is found. Lastly, interpolation search which is close to binary search, but it is executed with approximate location of where the data exists, just like how things work in dictionaries and bibles, when you want to search for Matius, of course you search at the new testament, not old testament, and if you want to search algorithm in your dictionary, of course you search at the letter T part,not A, B, or any other alphabets, but this only applies in dictionaries in the form of a book, not only ones. This is the sample code for bubble search and selection sort.
#include<stdio.h>
FILE *fp;
int number[100];
int n,minimum;
void bubble_sort()
{
int temp;
for(int i=0;i<n;i++)
{
for(int j=n-1;j>=i;j--)
{
if(angka[j-1]>angka[j])
{
temp=angka[j-1];
angka[j-1]=angka[j];
angka[j]=temp;
}
}
}
}
void selection_sort()
{
int temp;
for(int i=0;i<n;i++)
{
minimum=i;
for(int j=i+1;j<n;j++)
{
if(angka[min]>angka[j]) // if you want to search for maximum, just change it into <
{
minimum=j;
}
}
temp=angka[minimum];
angka[minimum]=angka[i];
angka[i]=temp;
}
}
void view_data()
{
for(int i=0;i<n;i++)
printf("%d ", angka[i]);
}
void read_file()
{
n=0;
fp=fopen("thisissorting.txt", "r");
while(!feof(fp))
{
fscanf(fp, "%d\n" , &angka[n]);
n++;
}
fclose(fp);
}
int main()
{
read_file();
view_data();
printf("\n");
bubble_sort();
view_data();
read_file();
view_data();
printf("\n");
selection_sort();
view_data();
return 0;
}
#include<stdio.h>
FILE *fp;
int number[100];
int n,minimum;
void bubble_sort()
{
int temp;
for(int i=0;i<n;i++)
{
for(int j=n-1;j>=i;j--)
{
if(angka[j-1]>angka[j])
{
temp=angka[j-1];
angka[j-1]=angka[j];
angka[j]=temp;
}
}
}
}
void selection_sort()
{
int temp;
for(int i=0;i<n;i++)
{
minimum=i;
for(int j=i+1;j<n;j++)
{
if(angka[min]>angka[j]) // if you want to search for maximum, just change it into <
{
minimum=j;
}
}
temp=angka[minimum];
angka[minimum]=angka[i];
angka[i]=temp;
}
}
void view_data()
{
for(int i=0;i<n;i++)
printf("%d ", angka[i]);
}
void read_file()
{
n=0;
fp=fopen("thisissorting.txt", "r");
while(!feof(fp))
{
fscanf(fp, "%d\n" , &angka[n]);
n++;
}
fclose(fp);
}
int main()
{
read_file();
view_data();
printf("\n");
bubble_sort();
view_data();
read_file();
view_data();
printf("\n");
selection_sort();
view_data();
return 0;
}
Luis Indracahya
2201758934
luis.indracahya@binus.ac.id
skyconnectiva.com
Komentar
Posting Komentar