Sunteți pe pagina 1din 4

What are primitive and non-primitive data types?

Answer: A data type is a classification of data, which can store a specific type of information.
Data types are primarily used in computer programming, in which variables are created to store
data. Each variable is assigned a data type that determines what type of data the variable may
contain.
The term "data type" and "primitive data type" are often used interchangeably. Primitive data
types are predefined types of data, which are supported by the programming language. For
example, integer, character, and string are all primitive data types. Programmers can use these
data types when creating variables in their programs. For example, a programmer may create a
variable called "lastname" and define it as a string data type. The variable will then store data as
a string of characters.
Non-primitive data types are not defined by the programming language, but are instead created
by the programmer. They are sometimes called "reference variables," or "object references,"
since they reference a memory location, which stores the data. In the Java programming
language, non-primitive data types are simply called "objects" because they are created, rather
than predefined. While an object may contain any type of data, the information referenced by the
object may still be stored as a primitive data type.

Bubble Sort C++


void bubbleSort(int arr[], int n) {
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];

arr[i] = arr[i + 1];


arr[i + 1] = tmp;
swapped = true;
}
}
}
}
The Algorithm:

The algorithm for this one is pretty straightforward as we shall see. Lets take a look at how its
done:
Linear Search Steps:
Step 1 - Does the item match the value Im looking for?
Step 2 - If it does match return, youve found your item!
Step 3 - If it does not match advance and repeat the process.
Step 4 - Reached the end of the list and still no value found? Well obviously the item is not in the
list! Return -1 to signify you have not found your value.

//linearSearch Function
int linearSearch(int data[],
02
int length, int val) {
03
04
for (int i = 0; i <= length; i++) {
05
if (val == data[i]) {
06
return i;
07
}//end if
08
}//end for
09
return -1;
//Value was not in the list
10 }//end linearSearch Function

Binary Search
binary_search(A, target):
lo = 1, hi = size(A)
while lo <= hi:
mid = lo + (hi-lo)/2
if A[mid] == target:
return mid
else if A[mid] < target:
lo = mid+1
else:
hi = mid-1
// target was not found
Matrix Multiplication

int main(void)
{
const int ROW=4, COL=4, INNER=4;
int A[ROW][INNER], int B[INNER][COL], int
C[ROW][COL];
for (int row = 0; row != ROW; ++row)
{
for (int col = 0; col != COL; ++col)
{
int sum = 0;
for (int inner = 0; inner != INNER;
++inner)
{
sum += A[row][inner] *
B[inner][col];
}
C[row][col] = sum;
}
}
return 0;
}

S-ar putea să vă placă și