How to shift 2d array elements down in C - c
I am trying to shift the contents of a 2d array down when implementing Tetris in C. This is to move the blocks down. The code works but its not moving elements once only, See the image for the problem(The number in the top left corner is the random number that determines the block type). Any help appreciated.
Below is the array shifting code:
//Declare size of board
int board [22][10] = {};
//Shift down
for(i=2;i<20;i++)
{
for(z=1;z<10;z++)
{
board[i+1][z] = board[i][z];
}
}
http://i61.tinypic.com/xlb58g.jpg
Whenever you shift the contents of an array, you must work in the opposite direction then the shifting. In your case, you need to invert the direction of your outer loop:
int board [22][10] = {};
for(i = 20; i-- > 2; ) {
for(z=1; z<9; z++) {
board[i+1][z] = board[i][z];
}
}
This allows the row of unused values to rise up in the array like a bubble.
Edit:
The code above was written to match the apparent intended behavior of the code posted in the question. If the entire array is to be moved, use this code:
for(i = sizeof(board)/sizeof(*board) - 1; i--; ) {
for(z = 0; z < sizeof(*board)/sizeof(**board); z++) {
board[i+1][z] = board[i][z];
}
}
Related
Most memory efficient algorithm for finding a path on a grid
What is the most memory efficient algorithm that can be used to find a path from one grid square to another? The grid may have obstacles that cannot be crossed. Being the shortest path is not necessary, but certainly, is a bonus. The algorithm is going to be coded in C (C++ is available, but I am avoiding it to reduce memory usage) and run on an ATmega328 chip with only 2048 bytes of SRAM. CPU efficiency is not of paramount importance. EDIT: The grid is 16 by 32 squares, each represented by one bit. The total memory usage is therefore 64 bytes. The grid is stored as a 2D array of unsigned chars and all of the 2048 bytes are available. The output would be an array of integers referencing the squares that should be taken. If there is an obstacle in a square, the array of squares would have a 1 instead of a zero. These squares should be treated like walls.
This is an unfinished idea for an algorithm which may fit into 2048 bytes, that I came up with while trying to find a non-recursive flood-fill variant. The first step is to create an additional 32×16 array of 8-bit values; this uses 512 bytes. You then iterate over the grid horizontally, and number the runs of adjacent reachable squares as in the image below: For a 32×16 grid, the maximum number of runs is 256 (e.g. with a checkerboard pattern, or vertical stripes), so this numbering fits into 8-bit values. The second step is to iterate over the grid vertically, and group the runs that are adjacent: After checking vertical line 1: {0A,11,1A} {2E} {44,50,5C} {72} {87,8F,98} After checking vertical line 2: {0A,11,1A,00,24} {2E} {44,50,5C,37,69} {72} {87,8F,98,7C} After checking vertical line 2: {0A,11,1A,00,24,12,2F} {2E} {44,50,5C,37,69,51,73} {72} {87,8F,98,7C,90} ... and so on, merging groups if they are linked by adjacent runs. If, at the end, the number of the start and target squares are in the same group, that means there is a path. Now, if you store the groups as simple lists, like in the example above, this doesn't really give you a path; it just tells you which squares are reachable from the start and target squares, but a path may not need to cross all these squares. If you stored the groups in a data structure where you know which runs are connected to each other, then it becomes a "shortest path through graph" problem in a smaller space. I'm not sure which data structure would best fit into the remaining 1536 bytes. (Anyone is welcome to try and take this idea further.) This method could be used to simplify the grid before running another algorithm. Firstly, the grouping of the runs identifies unreachable parts of the grid; these could be marked as walls in the original grid or a copy of it. Secondly, it identifies dead ends; runs which are only connected to one other run (and which don't contain the start or target square) are unnecessary detours and can also be marked as such. (This should be repeated: removing a singly-connected run may reveal another run to be singly-connected.) Grid simplified by removing unreachable and singly-linked runs Running the algorithm again, but with vertical runs and horizontal grouping, could remove additional dead ends. The JavaScript snippet below is a simple code example for the first part of the algorithm: using the example grid in the images, it numbers the runs, assigns them to groups, merges groups when necessary, and then checks whether the start and target square are in the same group, i.e. whether there is a path. The grouping method may not be the most efficient, especially when merging groups, but it uses a fixed-size array of maximum 256 bytes (number of runs × 8-bit values), which is probably best in a limited-memory situation. function gridPath(grid, x1, y1, x2, y2) { var runs = [], rcount = 0; for (var i = 0; i < 16; i++) { // number runs var start = true; runs[i] = []; for (var j = 0; j < 32; ++j) { if (grid[i][j] == 0) { // found empty cell if (start) ++rcount; // start of new run runs[i][j] = rcount - 1; start = false; } else start = true; // found blocked cell } } var groups = [], gcount = 0; for (var i = 0; i < rcount; i++) groups[i] = 0xFF; for (var j = 0; j < 32; ++j) { // assign runs to groups var g = []; for (var i = 0; i < 16; ++i) { if (grid[i][j] == 0) g.push(runs[i][j]); if ((grid[i][j] == 1 || i == 15) && g.length > 0) { insertGroup(g); g = []; } } } return groups[runs[y1][x1]] == groups[runs[y2][x2]]; function insertGroup(g) { var matches = []; for (var i = 0; i < g.length; i++) { // check if runs are already in group if (groups[g[i]] != 0xFF && matches.indexOf(groups[g[i]]) < 0) { matches.push(groups[g[i]]); } } if (matches.length == 0) matches.push(gcount++); // start new group for (var i = 0; i < g.length; i++) { // add runs to group groups[g[i]] = matches[0]; } if (matches.length > 1) { // merge groups for (var i = 0; i < rcount; i++) { if (matches.indexOf(groups[i]) > 0) groups[i] = matches[0]; } } } } var grid = [[1,0,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,1,0,0,0], [0,0,0,1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,1,0], [0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,0], [0,0,1,0,1,0,1,0,1,0,0,1,0,0,1,1,1,1,1,0,0,1,0,0,0,1,1,0,1,0,0,1], [1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,0,0,1,0,0,1,0,1,0,0,1,0,0,1,0], [0,1,0,0,0,1,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,1,1,0,0,0,0,0,1,0,1], [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,1,1,1,1,0,1,0], [0,0,0,1,0,0,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0,0], [0,1,0,0,0,1,0,0,0,1,1,0,1,0,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0], [0,0,1,0,1,0,1,0,1,0,1,0,0,1,0,0,0,1,0,0,1,0,1,0,1,0,0,1,0,1,0,0], [1,0,0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,0,0,1,0,0,1,0,0,0,1,0,1,0,0,1], [0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,0,1,0], [1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1,0,0,1,0,0,0,1,0,1,0,1,0,0,1], [0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0], [0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0], [0,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0]]; document.write(gridPath(grid, 0, 15, 15, 7));
If you only want to find the target, but do not care about remembering the path that was taken, then random search is pretty much optimal memory wise. It does not need to remember anything about previous states, so the memory use is constant. (Time complexity on the other hand is unbounded, which is not great, but isn't excluded by your requirements) If you do need to remember the taken path, then you cannot go below linear space complexity with an algorithm that is complete - i.e always finds a path if it exists. Both breadth and depth first searches have linear space complexity, so they would be asymptotically in the same class as the optimal complete algorithm. Since the memory is very limited, you might prefer to use a memory bounded algorithm, that gives you constant upper bound for memory use, but is not guaranteed to find a path that might exist. I recommend Simplified Memory Bounded A*.
I looked into using Dijkstra (as suggested by Weather Vane), which would require that for each grid cell the distance to the starting point and the direction from the previous cell is stored. Unfortunately, it is possible for paths on a 32x16 grid to have a distance greater than 255; the longest path I found has distance 319 (see image below, left). This means that the distances won't fit in 8-bits, and the distance matrix has a size of 1024 bytes. Left: longest path (distance=319). Right: largest number of equidistant cells (72 cells at distance 16) However, in a square grid where all distances equal 1, you can simplify Dijkstra to a breadth-first search which doesn't use a distance matrix; if you use a fifo queue, the cells are visited in order of distance to the starting cell, so you cannot find a shorter path to an already visited cell. The fifo queue will contain every cell at a certain distance, then gradually transition to distance + 1, and so on. The maximum size of the queue depends on how many equidistant cells there can be; the maximum I found is 72 (see image above, right) and during the transition from the previous distance this requires a queue that can hold the coordinates of 76 cells, or 152 bytes. The path which is returned by the algorithm is an array holding the coordinates of a maximum of 320 cells, so it has a maximum size of 640 bytes. Before constructing this array, the queue can be discarded, so only the direction grid and the path are in memory at the same time. Below is a code example of the simplified algorithm with only a direction matrix and a fifo queue; it can probably be improved on many points, but it demonstrates the idea. The findPath() function uses a minimum of 664 up to a maximum of 1152 bytes of allocated memory (depending on path length) plus around 20 bytes for additional variables. This could be further reduced, e.g. by storing the direction matrix as 4-bit nibbles, reducing its size from 512 to 256 bytes (but requiring more calculations), or by returning the path as a sequence of up/right/down/left directions instead of cell coordinates, which would require only 2 bits per step, reducing its maximum size from 640 to 80 bytes. #include <stdlib.h> // gcc -std=c99 short int findPath(char grid[][32], char x1, char y1, char x2, char y2, char **path) { char (*dir)[16][32] = calloc(512, 1); // allocate direction matrix: 512 bytes (zeros) (*dir)[y2][x2] = 5; // mark starting cell as visited (search backwards) char *queue = malloc(152); // allocate fifo queue: 152 bytes queue[0] = x2; queue[1] = y2; // put starting cell in queue (search backwards) unsigned char qRead = 0, qWrite = 2; // queue pointers char qCurSize = 1, qNextSize = 0; // queue size per distance short int distance = 0; // distance to current cell char dx[4] = {0, 1, 0, -1}; // up, right, down, left while (qRead != qWrite && !(*dir)[y1][x1]) { // until queue empty (fail) or target reached char x = queue[qRead++], y = queue[qRead++]; // take oldest cell from queue qRead %= 152; // wrap-around queue pointer for (char i = 0; i < 4; i++) { // check 4 neighbouring cells char nx = x + dx[i], ny = y + dx[3 - i]; // coordinates of neighbouring cell if (nx >= 0 && nx < 32 && ny >= 0 && ny < 16 // coordinates not off-grid && !grid[ny][nx] && !(*dir)[ny][nx]) { // traversable unvisited cell (*dir)[ny][nx] = i + 1; // store direction 1-4 queue[qWrite++] = nx; queue[qWrite++] = ny; // put cell in queue qWrite %= 152; // wrap-around queue pointer ++qNextSize; // increment queue size for next distance } } if (!--qCurSize || (*dir)[y1][x1]) { // current distance done or target reached qCurSize = qNextSize; // switch to distance + 1 qNextSize = 0; ++distance; } } free(queue); // free up queue memory for path if (!(*dir)[y1][x1]) distance = -1; // no path found else { // path found *path = malloc(distance * 2 + 2); // allocate path array: 2 bytes per step (*path)[0] = x1; (*path)[1] = y1; // starting position (forward) for (short int i = 1; i <= distance; i++) { // retrace steps char d = (*dir)[y1][x1] - 1; // direction of previous step 0-3 x1 -= dx[d]; y1 -= dx[3 - d]; // go back to previous position (*path)[i * 2] = x1; (*path)[i * 2 + 1] = y1; // add cell to path } } free(*dir); // discard direction matrix return distance + 1; // return number of cells in path } int main() { char grid[][32] = // max queue size: 76 {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; char x1 = 31, y1 = 0, x2 = 16, y2 = 7, *path = NULL; short int steps = findPath(grid, x1, y1, x2, y2, &path); // do stuff free(path); // discard path array return 0; }
Filling in random positions in a huge 2D array
Is a there a neat algorithm that I can use to fill in random positions in a huge 2D n x n array with m number of integers without filling in an occupied position? Where , and Kind of like this pseudo code: int n; int m; void init(int new_n, int new_m) { n = new_n; m = new_m; } void create_grid() { int grid[n][n]; int x, y; for(x = 1; x <= n; x ++) { for(y = 1; y <= n; y ++) { grid[x][y] = 0; } } populate_grid(grid); } void populate_grid(int grid[][]) { int i = 1; int x, y; while(i <= m) { x = get_pos(); y = get_pos(); if(grid[x][y] == 0) { grid[x][y] = i; i ++; } } } int get_pos() { return random() % n + 1; } ... but more efficient for bigger n's and m's. Specially if m is bigger and more positions are being occupied, it would take longer to generate a random position that isn't occupied.
Unless the filling factor really gets large, you shouldn't worry about hitting occupied positions. Assuming for instance that half of the cells are already filled, you have 50% of chances to first hit a filled cell; and 25% to hit two filled ones in a row; 12.5% of hitting three... On average, it takes... two attempts to find an empty place ! (More generally, if there is only a fraction 1/M of free cells, the average number of attempts raises to M.) If you absolutely want to avoid having to test the cells, you can work by initializing an array with the indexes of the free cells. Then instead of choosing a random cell, you choose a random entry in the array, between 1 and L (the lenght of the list, initially N²). After having chosen an entry, you set the corresponding cell, you move the last element in the list to the random position, and set L= L-1. This way, the list of free positions is kept up-to-date. Note the this process is probably less efficient than blind attempts.
To generate pseudo-random positions without repeats, you can do something like this: for (int y=0; y<n; ++y) { for(int x=0; x<n; ++x) { int u=x,v=y; u = (u+hash(v))%n; v = (v+hash(u))%n; u = (u+hash(v))%n; output(u,v); } } for this to work properly, hash(x) needs to be a good pseudo-random hash function that produces positive numbers that won't overflow when you add to a number between 0 and n. This is a version of the Feistel structure (https://en.wikipedia.org/wiki/Feistel_cipher), which is commonly used to make cryptographic ciphers like DES. The trick is that each step like u = (u+hash(v))%n; is invertible -- you can get your original u back by doing u = (u-hash(v))%n (I mean you could if the % operator worked with negative numbers the way everyone wishes it did) Since you can invert the operations to get the original x,y back from each u,v output, each distinct x,y MUST produce a distinct u,v.
Accessing portions of a dynamic array in C?
I know, another dynamic array question, this one is a bit different though so maybe it'll be worth answering. I am making a terrain generator in C with SDL, I am drawing 9 chunks surrounding the screen, proportional to the screen size, that way terrains can be generated easier in the future. This means that I have to be able to resize the array at any given point, so I made a dynamic array (at least according to an answer I found on stack it is) and everything SEEMS to work fine, nothing is crashing, it even draws a single tile....but just one. I am looking at it and yeah, sure enough it's iterating through the array but only writing to one portion of memory. I am using a struct called Tile that just holds the x, y, w, and h of a rectangle. This is the code I am using to allocate the array Tile* TileMap = (Tile*)malloc(0 * sizeof(Tile*)); int arrayLen = sizeof(TileMap); TileMap = (Tile*)realloc(TileMap, (totalTiles) * sizeof(Tile)); arrayLen = sizeof(totalTiles * sizeof(Tile)); The totalTiles are just the number of tiles that I have calculated previously are on the screen, I've checked the math and it's correct, and it even allocates the proper amount of memory. Here is the code I use to initialize the array: //Clear all elements to zero. for (int i = 0; i < arrayLen; i++) { Tile tile = {}; TileMap[i] = tile; } So what's weird to me is it is considering the size of a tile (16 bytes) * the totalTiles (78,000) is equaling 4....When I drill down into the array, it only has one single rect in it that gets cleared as well, so then when I go calculate the sizes of each tile: //Figure out Y and heights for (int i = startY; i <= (startY*(-1)) * 2; i += TILE_HEIGHT) { TileMap[i].y = i * TILE_HEIGHT; TileMap[i].h = TILE_HEIGHT; //Figure out X and widths for (int j = startX; j <= (startX*(-1)) * 2; j += TILE_WIDTH) { TileMap[i].x = i * TILE_WIDTH; TileMap[i].w = TILE_WIDTH; } } *Side note, the startX is the negative offset I am using to draw chunks behind the camera, so I times it by -1 to make it positive and then time it by two to get one chunk in front of the camera Alright, so obviously that only initializes one, and here is the render code for (int i = 0; i < totalTiles; i++) { SDL_Rect currentTile; currentTile.x = TileMap[i].x; currentTile.y = TileMap[i].y; currentTile.w = TileMap[i].w; currentTile.h = TileMap[i].h; SDL_RenderDrawRect(renderer, ¤tTile); } free(TileMap); So what am I doing wrong here? I mean I literally am just baffled right now...And before Vectors get recommended in place of dynamic arrays, I don't really like using them and I want to learn to deal with stuff like this, not just implement some simple fix.
Lots of confusion (which is commonplace with C pointers). The following code doesn't provide expected answer :arrayLen = sizeof(totalTiles * sizeof(Tile)); totalTiles * sizeof(Tile) is not even a type, I'm surprised it compiles at all. Edit : See molbnilo comment below. so it provides the size of the return type. Anyway, proper answer should be : arrayLen = totalTiles; Because that's what you need in your next loop : //Clear all elements to zero. for (int i = 0; i < arrayLen; i++) { Tile tile = {}; TileMap[i] = tile; } You don't need the size of the table, you need its number of elements. There are other confusions in your sample, they don't directly impact the rest of the code, but better correct them : Tile* TileMap = (Tile*)malloc(0 * sizeof(Tile*)); : avoid allocating a size of 0. int arrayLen = sizeof(TileMap); : no, it's not the arrayLen, just the size of the pointer (hence 4 bytes on 32-bits binaries). Remember TileMap is not defined as a table, but as a pointer allocated with malloc() and then realloc().
Linear algorithm to fill a contour
I am trying to make an algorithm that will fill a contour in linear complexity. I know such an algorithm exists. I've read somewhere that it has to do with the number of crossings, but there is a special case that I haven't had great luck in solving yet. So far I have tried using the following algorithm. Note that I can't access previous elements (to the left) because they will/may be overwritten: for (int y = blob->miny; y < blob->maxy; ++y) { int NumberOfBorderCrossings = 0; unsigned int NextElem = 0; unsigned int NextNextElem = 0; for (int x = blob->minx-1; x < blob->maxx-1; ++x) { NextElem = CV_IMAGE_ELEM(labelimg,unsigned int,y,x+1); NextNextElem = CV_IMAGE_ELEM(labelimg,unsigned int,y,x+2); if (CV_IMAGE_ELEM(labelimg,unsigned int,y,x) != label) { if (NextElem == label && NextNextElem != label) ++NumberOfBorderCrossings; else if (NumberOfBorderCrossings%2) CV_IMAGE_ELEM(labelimg,unsigned int,y,x) = label; } } } The result I get is the following. The input is to the right (all non-black pixels must be copied), and the erroneous output is to the left. Note again that I only have the contour of the image to the right (not rendered).
It appears that you're looking for a general Polygon filling algorithm. Your line crossing counting algorithm will break where it hits single points and horizontal and vertical lines. Have a look at Quickfill for a possible alternative.
Find the number of occurrence of each element in an array and update the information related to each elements
I have a big 2-D array, array[length][2]. the length= 500000. In array[i][0]= hex number, array[i][1]= 0 or 1, which represents some information related to each hex number. Like this: array[i][0] array[i][1] e05f56f8 1 e045ac44 1 e05f57fc 1 e05f57b4 1 e05ff8dc 0 e05ff8ec 0 e05ff900 1 I want to get a new array which stores: the hex number,# of occurance, the sum of array[i][1] of the same hex number. I write the code like this: //First Sort the array according to array[][0] int x,y,temp1,temp2; for (x=lines_num1-2;x>=0;x--) { for (y=0;y<=x;y++) { if(array[y][0]>array[y+1][0]) { temp1=array[y][0]; array[y][0]=array[y+1][0]; array[y+1][0]=temp1; temp2=array[y][1]; array[y][1]=array[y+1][1]; array[y+1][1]=temp2; } } } // generate the new_array[][] int new_array[length][3]; int n=0; for (n=0; n<length; n++){ new_array[n][0]=0; new_array[n][1]=0; new_array[n][2]=0; } int prev = array[0][0]; new_array[0][0]=array[0][0]; new_array[0][1]=1; new_array[0][2]=array[0][2]; for (k=1;k<length;k++) { if (array[k][0] == prev) { new_array[n][1]=new_array[n][1]+1; new_array[n][2]=new_array[n][2]+array[k][0]; }else{ prev = array[k][0]; new_array[n+1][0]=array[k][0]; new_array[n+1][1]=new_array[n+1][1]+1; new_array[n+1][2]=new_array[n+1][2]+array[k][0]; n++; } } But the code seems not work as I expected. First the sorting is so slow. And It seems cannot generate the correct new_array. Any suggestion on how to deal with this.
Personally, I would write a hash function to index the result array with the hexadecimal value directly. Then it is simple: struct { unsigned int nocc; unsigned int nsum; } result[/* ... */]; /* calculate the results */ for (i = 0; i < LENGTH; ++i) { int *curr = &array[i]; unsigned int index = hash(curr[0]); result[index].nocc++; result[index].nsum += curr[1]; } If you want to sort your array, don't reinventing the wheel: use qsort from the standard C library.
Sorting is slow because you're using bubble sort to sort the data. Bubble sort has quadratic average complexity, which means it has to perform more then 100 billion comparisons and swaps to sort your array. For this reason, never use bubble sort. Instead, learn to use the qsort library function and apply it to your problem. Also, your sorting code has at least one bug: when exchanging values for the second column of the array, you are getting the value with the wrong column index, [3] instead of [1].
For your scenario insertion sort is the right solution, while doing the insertion itself you could make the #count and the sum. When the sort is finished, you will have your result array as well. The code might look something like this int hex = 0, count = 0, sum = 0, iHole; for (i=1; i < lines_num1 -1; i++) { hex = array[i][0]; count = array[i][1]; sum = array[i][2]; iHole = i // keep moving the hole to next smaller index until A[iHole - 1] is <= item while (iHole > 0 and array[iHole - 1][0] > hex) { // move hole to next smaller index A[iHole][0] = A[iHole - 1][0]; A[iHole][1] = A[iHole - 1][1]; A[iHole][2] = A[iHole - 1][2]; iHole = iHole - 1 } // put item in the hole if (array[iHole][0] == hex) { array[iHole][1]++; array[iHole][2] += array[iHole][0]; } else { array[iHole][0] = hex; array[iHole][1] = 1; array[iHole][2] = hex; } } So the cost of making the second array is cost of the sorting itself. O(n) best case, O(n^2) worst case, and you don't have to travel again to make the sum and count. Remember this sort is a inplace sort. If you don't want to affect your original array that could be done as well with iHole pointing to the new array. The iHole should point to the tail of new array instead of "i"