Key field sorting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Key field sorting

Can anyone help me what does the term "key" mean in data structure and algorithms? I searched and i found something but i couldn't understand them . Sorting refers to the operation or technique of arranging and rearranging sets of data in some specific order. A collection of records called a list where every record has one or more fields. The fields which contain a unique value for each record is termed as the key field.

16th Mar 2020, 9:13 AM
Armina
Armina - avatar
12 Answers
+ 3
Having read your final paragraph, it seems the term "key" here describes something people commonly refer to as primary key - in regards to database table fields. A primary key, or as written in the paragraph - "key field" is a field which contains a unique value which represents a specific record Take an example of students data; where student ID number is unique. By referring a specific student ID number it should ideally be enough to refer a student record because of the uniqueness of the student ID number. I don't know where you read that from, but to my understanding, this is how I interpret the paragraph : )
16th Mar 2020, 10:12 AM
Ipang
+ 3
Armina 1. In regards to databases, the database designer decides which field should be primary key. There are methods commonly used in this assessment, and I hardly remember but to choose one that is unlikely to have duplicates. A web search on "database normalization" or "ERD" may provide you a better insight on how to assess and finally decide which field makes a suitable candidate. 2. Still in regards to databases, I believe each record should have same number of fields. Just as each student information may consist of same information e.g. their name, DoB etc. I think it also applies to data structure because database is an instance of data structure (a group of similar data records). Good reads BTW 👍
16th Mar 2020, 11:06 AM
Ipang
+ 3
No problem at all Armina 👌
16th Mar 2020, 11:12 AM
Ipang
+ 2
It means for example you have fields for a record are name, branch, marks. And if you want arrange this in assending order, then there may chance to have same values for all these. Then it becomes difficult to measure to sort condition. But if you add Id (or rank) as a field with each value unique 1,2,3,4,.... Then it becomes a key field for sorting your records. So you may consider as Id( or rank) field as key field for your sorting.. So key is the term which refers the factor on which basis sort will take place.. Hope this is understandable to you...
16th Mar 2020, 10:05 AM
Jayakrishna 🇮🇳
+ 2
Ipang Jayakrishna🇮🇳 Thanks 1. Is key referred (written) by us or does the complier itself decide the key for each record? 2. is number of fields for each record equal to ther record? ( i mean in a list of records can we can for a example 2 fields for one record and 3 fields for the other record? I read it from this website: Sorting Techniques in Data Structures https://www.w3schools.in/data-structures-tutorial/sorting-techniques/
16th Mar 2020, 10:24 AM
Armina
Armina - avatar
+ 2
Armina If am understood, correctly your question, 1)No. Compiler does what we say to do.. 2) No need same fields. That's why we have number of sorting techniques. We have to which one best suites. And for example simple sort or names Ex: (abcd, abc, ..) One length 3 other 4 still we need to sort it if accending then it should be abc, abcd. Same with different number of fields. Check out all those sorting techniques. You will get understand it more better. Also about key values they considering...
16th Mar 2020, 10:40 AM
Jayakrishna 🇮🇳
+ 2
Ipang Thanks alot for your help 🙏🙏
16th Mar 2020, 11:10 AM
Armina
Armina - avatar
+ 1
Jayakrishna🇮🇳 Thans alot , i will search them ; About my first question, for sorting an array , is the index of the array a key field ? (Can you give an example about assigning a key to record?)
16th Mar 2020, 10:53 AM
Armina
Armina - avatar
+ 1
Armina See this example program first loop, hoping that help you.. Read comments key factor there i mentioned.. Indexes in array not the key but value.. Indexes are already in sorted. https://code.sololearn.com/c9ueM82F1z40/?ref=app
16th Mar 2020, 11:35 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Thanks but i dont know Java language yet , Is it possible for you to write it in c++?
16th Mar 2020, 11:39 AM
Armina
Armina - avatar
+ 1
#include <iostream> using namespace std; int main() { int a[7]= {4, 1, 7, 9, 10, 11, 11}; int size=7; //size=sizeof(a)/sizeof(a[0]); for dinamic array size; for(int i=0;i<size;i++) cout<<a[i] <<" " ; for(int i=0;i<size;i++) for(int j=0;j<size;j++) if(a[i]>a[j]) //key technique for sorting array { int t=a[i]; a[i]=a[j]; a[j]=t; } cout<<endl<<"sorted in deccending order"<<endl; for(int i=0;i<size;i++) cout<<a[i] <<" " ; for(int i=0;i<size;i++) for(int j=0;j<size;j++) if(a[i]<=a[j]) //key to sort array { int t=a[i]; a[i]=a[j]; a[j]=t; } cout<<endl<<"sorted in accending order"<<endl; for(int i=0;i<size;i++) cout<<a[i] <<" " ; // return 0; } //Armina sry that privious one have some error that there is no length function in c++ so I edired it now. And You're Wel come..
16th Mar 2020, 1:05 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 Thanks alot 🙏🙏
16th Mar 2020, 12:50 PM
Armina
Armina - avatar